Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 16 Mar 2005
Offline Last Active Yesterday, 04:28 PM

#4900982 Multi Threading

Posted by rip-off on 09 January 2012 - 10:56 AM

If you are new to programming the very last thing you should be doing is thinking about multi-threading. Seriously - I do not mean to put you down, even advanced programmers have difficulty designing multi-threaded systems that are correct.

As for your question, by using the synchronized keyword inside the function body, one can be more explicit about the object being locked*. You can also reduce the area being locked to less than the entire function. Typically, you might only have a couple of lines that need co-ordination.

And no, threads don't move onto another method while the lock is held. Each thread blocks on the lock, causing them to stop running until the thread using the lock has relinquished it.

Even advanced programmers try to use higher level patterns (such as futures, thread pools, concurrent data structures) rather than trying to write things in terms of low level locking. For this reason, again I would advise you write non-multi-threaded programs first. The book is perhaps too advanced for you at the moment.

* Locking an arbitrary, publicly visibly object is dangerous and can lead to deadlocks if not handled carefully. A common idiom is to have a private final Object lock = new Object(), and synchronise on that.

#4900029 C++ Remove - deleting a file

Posted by rip-off on 05 January 2012 - 11:49 AM

When you open a file to write, it clears it. Then you try to delete it. According to the link you gave, remove() returns 0 on success. So your condition is wrong. Remove might not work because on some runtimes/operating systems, the file is "locked" while it is open.

Why even check if the file exists? Let the remove() function do that too!

Consider the following instead:
if(remove(fileName.c_str()) == 0) {
    output("Deletion successful\n");
} else {
    output("Deletion unsuccessful\n");

#4899554 Handling sprite (array or vector)

Posted by rip-off on 04 January 2012 - 05:37 AM

Don't use linked lists, regardless of how "groovy" they might appear, unless you have proof that they actually are what you want. For common usage patterns, a dynamic array will often have superior performance, even where one might not expect it due to the algorithmic complexity theory.

The algorithmic complexity theory is great, but it only makes sense for large N (typically, millions or more). For small N (like the thousands the OP is handling), the constants that are assumed by the theory to become irrelevant actually become more and more important - in some cases they can dominate. This is where you might often find the cache friendly vector outperforming linked lists for many operations on the kinds of data sets your program can handle.

Don't forget that a linked list implementation requires two algorithms, you have the basic linked list algorithm and the memory management algorithm. The latter is often ignored in highly theoretical discussions, but again it can make a massive difference.

Likewise, the dynamic array complexity ignores techniques like the "remove/erase idiom", which is often suitable for "bag" like data structures (append only, random remove). If the order is irrelevant, you can also use the "swap and pop" trick too.

Most games do not feature millions of sprites, because it would make the game unplayable. As such, you don't have to design the game to work with millions of sprites. If your game is working fine at the expected level of activity on the target hardware, you can stop. However, if you hadn't implemented, for example, collision and you went on to find that you aren't getting the required performance with either linked lists or vectors, you might need to reach for a more complex data structure, such as a spatial based one.

#4899216 Sharing a socket between processes VC++

Posted by rip-off on 03 January 2012 - 05:30 AM

I don't believe your approach will work. WSADuplicateSocket is intended for socket handoff, not socket sharing:

The descriptors that reference a shared socket can be used independently for I/O. However, the Windows Sockets interface does not implement any type of access control, so it is up to the processes involved to coordinate their operations on a shared socket. Shared sockets are typically used to having one process that is responsible for creating sockets and establishing connections, and other processes that are responsible for information exchange.

Maybe this is what you want to do?

If not, a simple solution to the original problem to that is to to have the secondary servers connect to the primary one, and have the primary one demultiplex the data as it comes in (however you propose to handle that) and copy it to the correct "local" connection to the secondary server.

One approach might be for the secondary server to connect to the problematic socket, and identify itself as a server somehow. The primary server would echo data back out that socket when it is intended for this sub-server. Another might use a local IPC mechanism, like a named pipe, to connect to the primary server, not unlike what you were trying to do with the socket description, but with the actual data instead.

That said, this is still highly unusual. Multiplexing connections like that is generally handled at a different level (e.g. a SSH tunnel), if it is indeed necessary at all.

#4898749 Java Game Programmer

Posted by rip-off on 01 January 2012 - 02:06 PM

60 fps is plenty - users will not be able to see any visual difference above this frame rate. The frame rate is probably vsync capped anyway. Either way, it is not a conclusive argument against using Java for games.

Generally one does not use AWT for games (unless they are the kind that lend themselves particularly to such a "standard" GUI, or a prototype). There are many libraries or frameworks which allow you to use Java with something like OpenGL for rendering.

#4897496 I'm currently working on a C-Like scripting language.

Posted by rip-off on 26 December 2011 - 03:40 PM

You can write game logic with C, but the whole reason I'm working on this project is for fun, really. The way I plan on implementing it into a project is by putting scripts into the map BSP, or allowing users to make mods.

Ok, I think I understand your design reasons a bit better now.

Besides more access to the memory, this scripting language will not make it easily possible to do something malicious.

Sure, but I believe you'll have your work cut out for you. Once you have relatively unrestricted access to memory, the script can easily crash the host application accidentally.

Well, I guess with some of the built in functions, you could theoretically delete system files...

If one is a malicious hacker (e.g. you can include scripts in custom maps that are then downloaded peer to peer) you have huge amounts of choice for exploiting the host. Not just deleting critical system files. One could drop some kind of system on the remote client.

... but I might add in some security to prevent things like that from happening.

It will be very difficult to sandbox the script in an a safe manner. It isn't realistic to believe you can just "add some security", the overall architecture of your application would need to be thought about carefully if you want to actually secure your application.

#4896274 [Java] Detecting collision between rectangles between classes

Posted by rip-off on 21 December 2011 - 01:34 PM

Something like this:
public class Main extends BasicGame{

    int px = 100, py = 100;
	private List<Entity> entities = new ArrayList<Entity>();
    Rectangle player = new Rectangle(px, py, 16, 16);
	public Main() {
	public static void main(String[] args) throws SlickException{
		AppGameContainer app = new AppGameContainer(new Main());

	public void render(GameContainer gc, Graphics g) throws SlickException {
		for(Entity entity : entities) {
			entity.render(gc, g);
		g.drawString("X:" + player.getX() + "\nY: " + player.getY(), 10, 40);
		//Draw player
		g.drawRect(px, py, 16 ,16);

	public void init(GameContainer gc) throws SlickException {
		Entity m1 = new Entity("Monster1", 100, 400, 200);
		Entity m2 = new Entity("Monster2", 100, 550, 200);
		Entity m3 = new Entity("Monster3", 100, 500, 300); 

	public void update(GameContainer gc, int arg) throws SlickException {
		// Movement
		if(Keyboard.isKeyDown(Keyboard.KEY_W)) py -= 0.1;
		else if(Keyboard.isKeyDown(Keyboard.KEY_S)) py ++;
		else if(Keyboard.isKeyDown(Keyboard.KEY_D)) px ++;
		else if(Keyboard.isKeyDown(Keyboard.KEY_A)) px -= 0.1;
		// Collision
		boolean collision = false;
		for(Entity entity : entities) {
				collision = true;
		if(collision) {
		} else {
			System.out.println("No collision");
I would recommend you research about containers, as they are the building blocks you'll need for any complex program.

#4896107 Critical library design question

Posted by rip-off on 21 December 2011 - 03:22 AM

It is an arbitrary convention. You might as well toss a coin.

I prefer +Y meaning UP, because it matches with standard mathematics. Then again, I like to use the centre of the screen as my origin, which I find convenient for doing small test programs (you need to do almost no work to get stuff drawn in the middle of the screen).

#4895709 Copying Legal Stuff

Posted by rip-off on 20 December 2011 - 08:17 AM

Don't write your own license, unless you are a lawyer.

Licences themselves fall under copywrite (unless you can find evidence to the contrary, e.g. that they have been donated to the public domain). For example, the wikipedia has a short section with regards to the copyright on the GPL:

The text of the GPL is itself copyrighted, and the copyright is held by the Free Software Foundation. However, the FSF does not hold the copyright for a work released under the GPL, unless an author explicitly assigns copyrights to the FSF (which seldom happens except for programs that are part of the GNU project). Only the individual copyright holders have the authority to sue when a license violation takes place.

The FSF permits people to create new licenses based on the GPL, as long as the derived licenses do not use the GPL preamble without permission. This is discouraged, however, since such a license might be incompatible with the GPL.[39] and causes a perceived license proliferation.
Other licenses created by the GNU project include the GNU Lesser General Public License and the GNU Free Documentation License.

Emphasis added.

#4895635 [Java] Detecting collision between rectangles between classes

Posted by rip-off on 20 December 2011 - 03:17 AM

You are instantiating new Entities in the render method. These entities are not tested for collision, because they do not exist in the scope of that method. Instead, you are using the Entity "eo", which isn't drawn.

Consider instantiating the Entities in the constructor or init() function, and placing them in a container e.g. ArrayList<Entity>. In the render method, loop through all the Entities and draw them. In the update method, loop through all the entities and test for collision.

#4895632 Where to start?

Posted by rip-off on 20 December 2011 - 03:05 AM

If you've already started Java, I would recommend you keep going. There is no absolute "better" programming language, there are only engineering trade-offs. It really doesn't matter too much which you start with, most programmers will learn dozens of languages eventually.

Also please note that the "For Beginners" Forum FAQ requests that you do not use the forum's "polling" feature in this sub-forum. Such polls are biased and become mere popularity contests, and do not give proper discussion and weight to the merits of the various choices.

#4895540 Mouse Events for a Board Game in SDL

Posted by rip-off on 19 December 2011 - 05:59 PM

Unfortunately, no such high level input exists in SDL. You need to do it yourself, or find a higher level library (possibly written in terms of SDL).

For instance, you could have a collection of rectangles, and write or find a function that can do a point/rectangle intersection test. You need to listen for mouse events, and test the mouse pointer position against this rectangle set when a click or drag is detected.

It is hard to give more than a broad overview to such a broad question. The details are up to you - but given how far you've gotten you should be capable. If you have a specific question about how to achieve any particular piece of this puzzle then it will be easier to provide a more detailed answer.

#4894219 [C] C compiler macro trouble

Posted by rip-off on 15 December 2011 - 11:24 AM

A simple way is just a counter of the current number of expansions. If it gets unreasonable, bail out.

Another way is to remember the order in which macros are defined. Only expand macros that were defined before the current one. This will prevent single recursive macros, and mutually recursive macros.

You might be able to use an existing C preprocessor, depending on what you are doing. There are typically flags you can pass to stop the compiler after the processing stage. Likewise, I'm sure there must be liberally licensed implementations of the preprocessor you can examine.

#4894208 virtual function

Posted by rip-off on 15 December 2011 - 10:52 AM

Like so: Base::myFunction(/* arguments ... */);

#4893880 Strange C++ Problem

Posted by rip-off on 14 December 2011 - 10:05 AM


$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright © 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

$ g++ *.cpp -lboost_thread -lgmp -Wall -Werror && ./a.out
big_integer.cpp: In member function ‘big_integer& big_integer::operator+=(const big_integer&)’:
big_integer.cpp:46:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator-=(const big_integer&)’:
big_integer.cpp:51:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator*=(const big_integer&)’:
big_integer.cpp:56:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator/=(const big_integer&)’:
big_integer.cpp:61:1: error: no return statement in function returning non-void [-Werror=return-type]
big_integer.cpp: In member function ‘big_integer& big_integer::operator%=(const big_integer&)’:
big_integer.cpp:66:1: error: no return statement in function returning non-void [-Werror=return-type]
cc1plus: all warnings being treated as errors