Re: How to make Application bundle from Executable? (Mac)

2015-02-20 Thread Gan via Digitalmars-d-learn
On Friday, 20 February 2015 at 17:28:48 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 06:19:29 UTC, Gan wrote:
On Friday, 20 February 2015 at 06:10:51 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 03:26:47 UTC, Gan wrote:
Also I can't get my application to load images that I place 
in the Resources folder(or any folder in the bundle for that 
matter).
I suggest to have a look at the projects generated by SFML 
regarding locating the resources in C++/ObjC and translate 
them to C/ObjC/D.
As for code (i.e frameworks and .dylibs) i don't know as 
shared libraries are still a murky area for D. Probably just 
better to stick to static libs.
Is there an official way to turn a D executable into a Mac 
Application Bundle?

Dunno

Good luck!


Frameworks aren't an issue, I put them into the Frameworks 
folder in my hand made bundle and they load fine.
When running the executable, it loads the image when it's in 
the same folder but when running the executable through the 
bundle, it doesn't find the image anywhere.


are you loading using relative or absolute addresses ( 
../../Resources/img.png or $BUNDLE_ROOT/Resources/img.png ) ?
also check the cwd when launched from the executable vs, the 
bundle. Also does Console give any output?


I fixed it by using:
string path = thisExePath();
int index = to!int(path.lastIndexOf(/));
if (!GameFont.loadFromFile(path[0 .. index]~/vertiup2.ttf)) {
writeln(Font failed to load);
}

Now my hand made bundle works fine and loads resources from 
inside the bundle.


Re: How to make Application bundle from Executable? (Mac)

2015-02-19 Thread Gan via Digitalmars-d-learn
On Friday, 20 February 2015 at 06:10:51 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 03:26:47 UTC, Gan wrote:
Also I can't get my application to load images that I place in 
the Resources folder(or any folder in the bundle for that 
matter).
I suggest to have a look at the projects generated by SFML 
regarding locating the resources in C++/ObjC and translate them 
to C/ObjC/D.
As for code (i.e frameworks and .dylibs) i don't know as shared 
libraries are still a murky area for D. Probably just better to 
stick to static libs.
Is there an official way to turn a D executable into a Mac 
Application Bundle?

Dunno

Good luck!


Frameworks aren't an issue, I put them into the Frameworks folder 
in my hand made bundle and they load fine.
When running the executable, it loads the image when it's in the 
same folder but when running the executable through the bundle, 
it doesn't find the image anywhere.


How to make Application bundle from Executable? (Mac)

2015-02-19 Thread Gan via Digitalmars-d-learn
I managed to copy an application bundle and change stuff inside 
it to run my executable, but it was very manual and kinda 
hackish. Also I can't get my application to load images that I 
place in the Resources folder(or any folder in the bundle for 
that matter).



Is there an official way to turn a D executable into a Mac 
Application Bundle?


Re: Better native D 2D graphics library?

2015-02-08 Thread Gan via Digitalmars-d-learn

On Sunday, 8 February 2015 at 09:52:50 UTC, Namespace wrote:

On Sunday, 8 February 2015 at 01:39:19 UTC, Gan wrote:

On Saturday, 7 February 2015 at 23:29:01 UTC, Namespace wrote:

On Saturday, 7 February 2015 at 22:09:03 UTC, Gan wrote:

Is there a better D graphics library in the works?

I'm using SFML(which is very easy and has lots of features) 
but it seems to use a lot of ram(if you leave it running for 
a while on a graphic intensive scene) and trying to make it 
include the dependencies with the compiled executable is 
complicated.


Is there a D 2D graphics library that's just as easy, cross 
platform, doesn't use X11, allows drawing to off-screen 
buffers and drawing those to screen? (plus supports nice 
drawing of shapes, circles, rectangles, lines)


I'm probably asking too much- I doubt such a thing exists.


I once wrote such a library: https://github.com/Dgame/Dgame
But since I left D I don't maintain it. Maybe that will 
change in the next few weeks. But otherwise you are free to 
use or improve it.


That's really cool. Very very similar to SFML.

Only thing that makes me concerned is:
static immutable string Disk = D;
static immutable string Mode = Release;

pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictSDL2.lib);
pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictUtil.lib);
pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictGL3.lib);


pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameInternal.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameAudio.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameGraphics.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameSystem.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameMath.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode 
~ \\DgameWindow.lib);


I'm not entirely sure what that is or if it's cross-compatible 
friendly or sharing-with-friends friendly.

Though I really hope you re-maintain it.


It worked on Mac, Linux and Windows so far without any problems.
Forget to add the documentation page I made: 
http://rswhite.de/dgame4/


Very cool. I was debating switching to SDL but now I'm only 
debating between switching from SFML to DGame.


When I leave my SFML game running for 7 hours, it uses 700mb of 
ram. Also my friend can't play my games due to missing dylibs(the 
SFML setup process is complicated). Those are the main reasons 
I'm looking for an alternative.
I'll do a DGame test, if it beats SFML on those issues- I'm 
hooked.


Re: Better native D 2D graphics library?

2015-02-07 Thread Gan via Digitalmars-d-learn

On Saturday, 7 February 2015 at 23:29:01 UTC, Namespace wrote:

On Saturday, 7 February 2015 at 22:09:03 UTC, Gan wrote:

Is there a better D graphics library in the works?

I'm using SFML(which is very easy and has lots of features) 
but it seems to use a lot of ram(if you leave it running for a 
while on a graphic intensive scene) and trying to make it 
include the dependencies with the compiled executable is 
complicated.


Is there a D 2D graphics library that's just as easy, cross 
platform, doesn't use X11, allows drawing to off-screen 
buffers and drawing those to screen? (plus supports nice 
drawing of shapes, circles, rectangles, lines)


I'm probably asking too much- I doubt such a thing exists.


I once wrote such a library: https://github.com/Dgame/Dgame
But since I left D I don't maintain it. Maybe that will change 
in the next few weeks. But otherwise you are free to use or 
improve it.


That's really cool. Very very similar to SFML.

Only thing that makes me concerned is:
static immutable string Disk = D;
static immutable string Mode = Release;

pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictSDL2.lib);
pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictUtil.lib);
pragma(lib, Disk ~ 
:\\D\\dmd2\\src\\ext\\derelict\\lib\\dmd\\DerelictGL3.lib);


pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameInternal.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameAudio.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameGraphics.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameSystem.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameMath.lib);
pragma(lib, Disk ~ :\\D\\dmd2\\src\\ext\\Dgame\\lib\\ ~ Mode ~ 
\\DgameWindow.lib);


I'm not entirely sure what that is or if it's cross-compatible 
friendly or sharing-with-friends friendly.

Though I really hope you re-maintain it.


Better native D 2D graphics library?

2015-02-07 Thread Gan via Digitalmars-d-learn

Is there a better D graphics library in the works?

I'm using SFML(which is very easy and has lots of features) but 
it seems to use a lot of ram(if you leave it running for a while 
on a graphic intensive scene) and trying to make it include the 
dependencies with the compiled executable is complicated.


Is there a D 2D graphics library that's just as easy, cross 
platform, doesn't use X11, allows drawing to off-screen buffers 
and drawing those to screen? (plus supports nice drawing of 
shapes, circles, rectangles, lines)


I'm probably asking too much- I doubt such a thing exists.


ubyte array to uint?

2015-02-05 Thread Gan via Digitalmars-d-learn

Is there a simple way of conversion? Something like:
uint length = to!uint(buffer[0 .. 4]);

Right now I have:
uint length = *cast(uint*)buffer[0 .. 4].ptr;

Which I'm not entirely sure is the correct way to do that.


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 01:36:17 UTC, Mike Parker wrote:

On 2/6/2015 9:50 AM, Gan wrote:
On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe 
wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:
Or am I misunderstanding the receive function? Does it send 
whole

messages or just message chunks?


It sends as much as it can when you call it. So if there's 
only 12
bytes available when you send it with a 4096 buffer, it will 
fill the
first twelve bytes (and return 12) so you can slice it 
buffer[0 ..

returnedValue] to get the data.

If there's more available than the buffer will hold, that 
data will be
held on to until next time you call receive. If 5000 bytes 
come off
the network, it will return 4096 the first time, then next 
time

through the loop, it will return the remaining 904.


How can you distinguish between different packets that get 
sent? Won't

they all be merged to a giant blob of data?


You need to give each of your packets a header. At a minimum 
you'll want a message ID and message length. When a message 
comes in, you use the length field to determine where one 
packet ends and the next one begins.


Oh sweet. Though if one message length is off by even 1 byte, 
then all future messages get corrupted?


Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn
I managed to get the client to connect but immediately on the 
server side, this happens:


long length = player.playerSocket.receive(buf);
if (length == 0) { //No longer connected
player.playerSocket.shutdown(SocketShutdown.BOTH);
player.playerSocket.close();
}

The player's socket receives a 0 length which means the 
connection is dead. Why does that happen?


Here's my full server-side networking code:

module server.networkingandio.networkingcontroller;
import server.server;
import server.player.player;

import std.socket;
import std.stdio;
import core.thread;
import std.range;
import std.traits;
import std.typecons;
import std.typetuple;
import std.algorithm;
import std.concurrency;

class NetworkingController
{
Server server;

Player[] players;

TcpSocket socket;

this(Server s)
{
server = s;
players = new Player[](0);
writeln(Server active on port );

socket = new TcpSocket();
socket.bind(new InternetAddress());
socket.listen(100);
socket.blocking = true;

spawn(handleNewConnectionsThread, cast(shared)socket);
}
void logic() {
//Check for new sockets
bool receivedNewSocket = true;
while (receivedNewSocket) {
receivedNewSocket = receiveTimeout(0.msecs,
(shared NewConnectionMsg message) {
NewConnectionMsg msg = 
cast(NewConnectionMsg)message;
newConnection(msg.socket);
},
(shared ReceiveDataMsg message) {
	//Convert data to MessageReader, send to server message 
handler

}
);
}

//Check for dead sockets
for (int i = 0; i  players.length; i++) {
Player p = players[i];
if (p.playerSocket.isAlive == false) {
players = players.remove(i);
i--;
server.removeConnection(p);
}
}
//Check for socket messages
}
void newConnection(Socket conn) {
Player p = new Player(conn, this);
players ~= p;
server.newConnection(p);
//Set up listener for player socket
spawn(handleReceiveDataThread, cast(shared)p);
}
}
class NewConnectionMsg {
Socket socket;
this(Socket socket) {
this.socket = socket;
}
}
class ReceiveDataMsg {
Player player;
ubyte[] data;
this(Player player, ubyte[] data) {
this.player = player;
this.data = data;
}
}

void handleNewConnectionsThread(shared TcpSocket s) {
Socket socket = cast(Socket)s;
while(socket.isAlive) {
Socket playerSocket = socket.accept();
if (playerSocket !is null  playerSocket.isAlive == true) {
playerSocket.blocking = true;
ownerTid.send(cast(shared) new 
NewConnectionMsg(playerSocket));
}
}
scope(exit)socket.close();
}

void handleReceiveDataThread(shared Player p) {
Player player = cast(Player)p;
while(player.playerSocket.isAlive) {
ubyte[] buf = new ubyte[](0);
long length = player.playerSocket.receive(buf);
if (length == 0) { //No longer connected
player.playerSocket.shutdown(SocketShutdown.BOTH);
player.playerSocket.close();
} else {
ownerTid.send(cast(shared) new ReceiveDataMsg(player, 
buf));
}
}
scope(exit)player.playerSocket.close();
}


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:24:54 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:15:15 UTC, Gan wrote:

ubyte[] buf = new ubyte[](0);


This is your problem: receive fills a preexisting buffer, and 
you allocated zero bytes for it to fill, so it can't give you 
anything.


Give it a bigger buffer, I like to use 4096, and it will work 
better.


Will there ever be a message bigger than 4096 bytes?


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:28:00 UTC, Gan wrote:

On Friday, 6 February 2015 at 00:24:54 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:15:15 UTC, Gan wrote:

ubyte[] buf = new ubyte[](0);


This is your problem: receive fills a preexisting buffer, and 
you allocated zero bytes for it to fill, so it can't give you 
anything.


Give it a bigger buffer, I like to use 4096, and it will work 
better.


Will there ever be a message bigger than 4096 bytes?


Or am I misunderstanding the receive function? Does it send whole 
messages or just message chunks?


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:
Or am I misunderstanding the receive function? Does it send 
whole messages or just message chunks?


It sends as much as it can when you call it. So if there's only 
12 bytes available when you send it with a 4096 buffer, it will 
fill the first twelve bytes (and return 12) so you can slice it 
buffer[0 .. returnedValue] to get the data.


If there's more available than the buffer will hold, that data 
will be held on to until next time you call receive. If 5000 
bytes come off the network, it will return 4096 the first time, 
then next time through the loop, it will return the remaining 
904.


How can you distinguish between different packets that get sent? 
Won't they all be merged to a giant blob of data?


Concurrent Thread Safe List?

2015-02-04 Thread Gan via Digitalmars-d-learn
I'm looking for a non-blocking way of a thread pushing objects 
into a list and another thread able to pull objects from the same 
list. Thread 1 pushes objects onto the list, Thread 2 pulls the 
oldest objects off the list.


Does D language have something like that?


Re: Concurrent Thread Safe List?

2015-02-04 Thread Gan via Digitalmars-d-learn

On Wednesday, 4 February 2015 at 22:14:31 UTC, Ali Çehreli wrote:

On 02/04/2015 12:10 PM, Gan wrote:
I'm looking for a non-blocking way of a thread pushing objects 
into a
list and another thread able to pull objects from the same 
list. Thread
1 pushes objects onto the list, Thread 2 pulls the oldest 
objects off

the list.

Does D language have something like that?


The std.concurrency module does exactly that:

  http://dlang.org/phobos/std_concurrency.html

And something I wrote:

  http://ddili.org/ders/d.en/concurrency.html

Ali


Cool article. First half of the article I was thinking, this 
isn't what I want. Then in the second half where you got into the 
mailbox analogy, that's exactly what I want.


Thanks.


Re: Concurrent Thread Safe List?

2015-02-04 Thread Gan via Digitalmars-d-learn

On Thursday, 5 February 2015 at 02:13:23 UTC, Gan wrote:
On Wednesday, 4 February 2015 at 22:14:31 UTC, Ali Çehreli 
wrote:

On 02/04/2015 12:10 PM, Gan wrote:
I'm looking for a non-blocking way of a thread pushing 
objects into a
list and another thread able to pull objects from the same 
list. Thread
1 pushes objects onto the list, Thread 2 pulls the oldest 
objects off

the list.

Does D language have something like that?


The std.concurrency module does exactly that:

 http://dlang.org/phobos/std_concurrency.html

And something I wrote:

 http://ddili.org/ders/d.en/concurrency.html

Ali


Cool article. First half of the article I was thinking, this 
isn't what I want. Then in the second half where you got into 
the mailbox analogy, that's exactly what I want.


Thanks.


If I want a non blocking receive call, would I do bool 
receiveTimeout(T...)(Duration duration, T ops); with a duration 
of 0?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 15:45:47 UTC, bearophile wrote:

Gan:


How can I make it use less CPU/RAM?


Most tiny classes probably should be structs. More generally, 
use a struct every time you don't need a class.


You can start with those two:

struct SBRange {
double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0;
}

struct Point(T) {
T x, y;
}

This probably isn't enough to solve your problems, but it's a 
start.


Bye,
bearophile


Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


On a second question, do I ever need to manually release objects 
I create with new?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


Most of your usages of tiny structs should be by value. So just 
keep in mind they are values. Even when you iterate with a 
foreach on a mutable array of them :-)



On a second question, do I ever need to manually release 
objects I create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only 
keeps 15 objects stored in the arrays at a time so why do you 
think my ram usage increases to 700+ after many hours?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep in mind they are values. Even when you iterate with 
a foreach on a mutable array of them :-)



On a second question, do I ever need to manually release 
objects I create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only 
keeps 15 objects stored in the arrays at a time so why do you 
think my ram usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to 
structs on Point and Range. Though my memory still climbs high.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile 
wrote:

Gan:

Is there some special stuff I gotta do extra with structs? 
Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you 
think my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed 
to structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the 
beginning too.


I did a test and ran GC.collect() every loop but my memory 
usage continues to rise. I can see how you'd be able to lower 
CPU usage by running GC.collect() every now and then but right 
now I'm stuck on the memory issue.


Perhaps my problem lies in the C++ library SFML?


I commented out some stuff and it appears my massive memory 
consumption comes from my tile.redraw function:


void redraw() {
undrawn = false;
canvas.clear();

//Add stars
for (int i = 0; i  controller.starsPerTile; i++) {
Random gen;
gen.seed(unpredictableSeed);
int x = uniform(0, controller.tileSize, gen);
int y = uniform(0, controller.tileSize, gen);
double s = uniform(0, 1, gen) / 1.0;
			double size = (s * (controller.starSizeMax - 
controller.starSizeMin)) + controller.starSizeMin;

if (x - size / 2  0) {
x += size / 2;
}
if (y - size / 2  0) {
y += size / 2;
}
if (x + size / 2  controller.tileSize) {
x -= size / 2;
}
if (y + size / 2  controller.tileSize) {
y -= size / 2;
}
drawStar(canvas, x, y, size, size);
}

canvas.display();
}
	void drawStar(RenderTarget target, int centerX, int centerY, 
double width, double height) {

CircleShape star;
if (controller.multiColor == false) {
star = controller.stars[0];
} else {
Random gen;
gen.seed(unpredictableSeed);
			star = controller.stars[uniform(0, controller.stars.length - 
1, gen)];

}
star.position = Vector2f(centerX, centerY);
star.origin = Vector2f(0.5, 0.5);
star.scale = Vector2f(width / 100.0, height / 100.0);
target.draw(star);
}


Would you know why this is using hundreds of mb of rams?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? 
Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you think 
my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to 
structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the beginning 
too.


I did a test and ran GC.collect() every loop but my memory usage 
continues to rise. I can see how you'd be able to lower CPU usage 
by running GC.collect() every now and then but right now I'm 
stuck on the memory issue.


Perhaps my problem lies in the C++ library SFML?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 22:42:25 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 11:39 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile 
wrote:

Gan:

Is there some special stuff I gotta do extra with 
structs? Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. 
So just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you 
think my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed 
to structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the 
beginning too.


I did a test and ran GC.collect() every loop but my memory 
usage
continues to rise. I can see how you'd be able to lower CPU 
usage by
running GC.collect() every now and then but right now I'm 
stuck on the

memory issue.

Perhaps my problem lies in the C++ library SFML?


I commented out some stuff and it appears my massive memory 
consumption

comes from my tile.redraw function:

void redraw() {
undrawn = false;
canvas.clear();

//Add stars
for (int i = 0; i  controller.starsPerTile; i++) {
Random gen;
gen.seed(unpredictableSeed);
int x = uniform(0, controller.tileSize, gen);
int y = uniform(0, controller.tileSize, gen);
double s = uniform(0, 1, gen) / 1.0;
double size = (s * (controller.starSizeMax -
controller.starSizeMin)) + controller.starSizeMin;
if (x - size / 2  0) {
x += size / 2;
}
if (y - size / 2  0) {
y += size / 2;
}
if (x + size / 2  controller.tileSize) {
x -= size / 2;
}
if (y + size / 2  controller.tileSize) {
y -= size / 2;
}
drawStar(canvas, x, y, size, size);
}

canvas.display();
}
void drawStar(RenderTarget target, int centerX, int 
centerY, double

width, double height) {
CircleShape star;
if (controller.multiColor == false) {
star = controller.stars[0];
} else {
Random gen;
gen.seed(unpredictableSeed);
star = controller.stars[uniform(0, 
controller.stars.length

- 1, gen)];
}
star.position = Vector2f(centerX, centerY);
star.origin = Vector2f(0.5, 0.5);
star.scale = Vector2f(width / 100.0, height / 100.0);
target.draw(star);
}


Would you know why this is using hundreds of mb of rams?


Try with only drawStart stripped.
If it still does it, then its redraw. Also try with just a hard 
coded call to drawStart.

Something smells wrong here.


Without drawStar, my program starts at 24mb and slowly increases. 
With drawStar my program starts at 70mb and increases at twice 
the rate.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 02:50:11 UTC, FG wrote:

On 2015-01-28 at 03:04, Vladimir Panteleev wrote:

What type is CircleShape?
If it is a class, or otherwise contains pointers, then this is 
probably the source of your problem.


class CircleShape : Shape is defined in 
dsfml.graphics.circleshape, so there's no going around this...


- Building your program for x86_64 - 64 bits of address space 
will make fake pointer pinning very unlikely


The binary included in the zip was 64-bit, so fake pointers 
shouldn't be that much of a problem.


Oh, and I take back what I said about suspecting that 
SpaceBackground.stack grows infinitely. It probably doesn't. I 
don't have DSFML installed, and therefore couldn't recreate the 
behaviour.


Is there a way to set a variable to be cleared in the new GC 
collection or to forcibly release a variable?


I have an inkling that it might be the Sprite class or something 
with the RenderTextures.


I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
I feel like the only way I can get better right now is if someone 
with more knowledge can give me some advice on the code I have 
written.


Here's the link: http://cl.ly/0s0Q1L1S3v0E

How can I make it use less CPU/RAM?
(Most code is in the Misc/SpaceBackground.d)


Re: Array List object?

2015-01-26 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 06:16:03 UTC, H. S. Teoh wrote:
On Tue, Jan 27, 2015 at 06:02:38AM +, Gan via 
Digitalmars-d-learn wrote:

On Tuesday, 27 January 2015 at 06:00:50 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 05:32:09 UTC, Gan wrote:
Hey I'm using normal arrays for my project:
//Declaring the array
SBTile[] tiles;

//Initializing the array
tiles = new SBTile[](0);

//Clearing the array
tiles = [];

//Removing a tile at index i from the array
tiles.remove(i);

//Adding a tile to the array
tiles ~= tile;

But I think I'm doing something very wrong because my list 
of tiles
is growing larger and larger. Am I misusing the array or is 
there a

better way of doing those array list functions?

Found my problem. When you call remove, you need to set it.

tiles = tiles.remove(i);

Though isn't it incredibly inefficient to continually have it
re-create the arrays for adding and removing? I'm asking 
cause I'm

not very knowledgable on this subject.

On a side note, my program's ram usage is increasing very 
rapidly. All

I'm doing to adding and removing objects from an array.


The answers to your question can be found here:

http://dlang.org/d-array-article.html

In short, D arrays are actually not arrays directly, but 
slices of
arrays (i.e., pointer and length pairs to a segment of memory 
managed by
the GC).  Therefore, assigning the return value of .remove back 
to tiles
is extremely efficient, because all you're doing is updating 
the .ptr
and .length fields of tiles. There is no copying of array 
elements at

all (except what's already being done in .remove).

However, the problem comes when you call .remove immediately 
followed by
~=. Because the runtime doesn't know whether you have made 
other slices
of the same array in the meantime, it doesn't know whether you 
wish to
retain the original array elements, so to be safe, whenever the 
array
length shrinks it assumes that the next time you append 
something new,
it should reallocate. Thus, every time ~= follows .remove, the 
array
will be reallocated, which is extremely slow. The solution is 
to tell
the runtime that yes, you do wish to overwrite whatever may 
have been

there in GC memory before when you append:

tiles = tiles.remove(i);
assumeSafeAppend(tiles); // --- this is the secret
tiles ~= tile;  // now this won't reallocate everytime

Note that append to the array will still reallocate 
occasionally (e.g.,
when there is no more space in the currently allocated GC 
block, and the
array needs to be moved to a new memory location where a bigger 
block
can be allocated). But it should perform a lot better than 
reallocating

every single time you append.


T


Thanks! My project use to hover at 80mb ram usage, now it hovers 
at 35mb ram usage. Nevermind. It just climbed up to 65mb


Okay now I'm very confused. When I have my program fully hidden 
behind another window, my ram usage goes up without going down. 
Which my program is partly visible it goes up a few mb then 
returns to the past amount of mb.


Is this a bug?


Re: Array List object?

2015-01-26 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 05:32:09 UTC, Gan wrote:

Hey I'm using normal arrays for my project:
//Declaring the array
SBTile[] tiles;

//Initializing the array
tiles = new SBTile[](0);

//Clearing the array
tiles = [];

//Removing a tile at index i from the array
tiles.remove(i);

//Adding a tile to the array
tiles ~= tile;

But I think I'm doing something very wrong because my list of 
tiles is growing larger and larger. Am I misusing the array or 
is there a better way of doing those array list functions?


Found my problem. When you call remove, you need to set it.

tiles = tiles.remove(i);

Though isn't it incredibly inefficient to continually have it 
re-create the arrays for adding and removing? I'm asking cause 
I'm not very knowledgable on this subject.


Re: Array List object?

2015-01-26 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 06:00:50 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 05:32:09 UTC, Gan wrote:

Hey I'm using normal arrays for my project:
//Declaring the array
SBTile[] tiles;

//Initializing the array
tiles = new SBTile[](0);

//Clearing the array
tiles = [];

//Removing a tile at index i from the array
tiles.remove(i);

//Adding a tile to the array
tiles ~= tile;

But I think I'm doing something very wrong because my list of 
tiles is growing larger and larger. Am I misusing the array or 
is there a better way of doing those array list functions?


Found my problem. When you call remove, you need to set it.

tiles = tiles.remove(i);

Though isn't it incredibly inefficient to continually have it 
re-create the arrays for adding and removing? I'm asking cause 
I'm not very knowledgable on this subject.


On a side note, my program's ram usage is increasing very 
rapidly. All I'm doing to adding and removing objects from an 
array.


Re: Conflicts with Import error - New to D, trying to build a new project

2015-01-25 Thread Gan via Digitalmars-d-learn

Thanks. I didn't realize that could conflict.

On Sunday, 25 January 2015 at 21:22:50 UTC, Ali Çehreli wrote:

On 01/25/2015 11:30 AM, Gan wrote:

Here's a screenshot: http://cl.ly/image/2n282v0B1X2M

The error is: 
/Users/Matt/Projects/spacecraft/source/Game/Game.d(0,0):
Error: class Game.Game.Game conflicts with import 
Game.Game.Game at

source/Game/Game.d(2) (spacecraft)

I figure it's because I did imports wrong or something. I'm 
still very

new to D. Can anyone help?


The problem is with having three constructs with the same name: 
package, module, and class.


I would use lowercase for package, and module names, and 
differentiate between the package and the module:


.../source/foogame/game.d

module foogame.game;

class Game
{
// ...
}

Ali




Question about Allocating

2015-01-25 Thread Gan via Digitalmars-d-learn
I've been working on my game and am getting some pretty gnarly 
memory problems. I think it's how I'm allocating.


Sometimes when I use variables I can do Color(255, 255, 255). But 
why is that different than new Color(255, 255, 255)?

Same when I'm making arrays. new int[](0) vs [].


What's the difference?


Re: Turning Executable into Application?

2015-01-25 Thread Gan via Digitalmars-d-learn

On Monday, 26 January 2015 at 06:37:34 UTC, tcak wrote:

On Monday, 26 January 2015 at 03:36:32 UTC, Gan wrote:
With Xamarin Studio I create a D project and run it. It runs 
an Executable Unix file through the terminal. How can I turn 
that into an Application that doesn't open the Terminal?


Thanks.


I use MonoDevelop. I haven't tried that but in Project Options 
(Not Solution Options), there is a checkbox saying Run on 
External Console. Uncheck that one maybe.


As far as I understand, you want to make a GUI program by 
saying application.


That's it! Thank you!


Turning Executable into Application?

2015-01-25 Thread Gan via Digitalmars-d-learn
With Xamarin Studio I create a D project and run it. It runs an 
Executable Unix file through the terminal. How can I turn that 
into an Application that doesn't open the Terminal?


Thanks.


Conflicts with Import error - New to D, trying to build a new project

2015-01-25 Thread Gan via Digitalmars-d-learn

Here's a screenshot: http://cl.ly/image/2n282v0B1X2M

The error is: 
/Users/Matt/Projects/spacecraft/source/Game/Game.d(0,0): Error: 
class Game.Game.Game conflicts with import Game.Game.Game at 
source/Game/Game.d(2) (spacecraft)


I figure it's because I did imports wrong or something. I'm still 
very new to D. Can anyone help?