Strange dev-cpp error

23/02/2010

Just made a test in Dev-cpp of a small bit of a sender routine. I got this error:
undefined  reference to ‘__cpu_features_init’
then all removed with an practically empty main.c, again the same error.

Found this link with the solution:
http://www.allegro.cc/forums/thread/598060

the solution being: Tools->Compiler Options->Directories->Libraries — delete all what is there and voila!


Issue when declaring functionpointer

29/01/2010

I have two functions:
void emptyFunction(int functionChoice)
{
}

void randomizeButtons(Button* array[], u8 items)
{
int counter;
for(counter=0; counter<items; counter++)
{
array[counter]->posX = 220*rand();
array[counter]->posY = 220*rand();

drawButton(array[counter]);
}
}

And two pointers to these functions:
void (*foo)(int);
foo = &emptyFunction;

void (*foo2)(Button*, u8);
foo2 = &randomizeButtons;

The second pointer though, gives a warning:
warning: assignment from incompatible pointer type

To solve this, for the second pointer only, I have to change the function declaration OR typecast to (void*).
So solution 1:
void (*foo2);

And solution 2:
foo2 = (void*) &randomizeButtons;

Very strange, because this isn’t needed for the first functionpointer, foo.


expected specifier-qualifier-list before ‘Button’

29/01/2010

What if you make a new struct, wich has a structmember that is a pointer to something that has the same type of the struct you are making?

For example:
typedef struct
{
char* text;
u8 posX;
u8 posY;
u8 width;
u8 height;
void* callback_function;
Button* array;
u8 itemsInArray;
} Button;

Now you will get the following error:  expected specifier-qualifier-list before ‘Button’
How to work around this?
Put struct in front of the structmember, and it will compile!

Working example:
ypedef struct
{
char* text;
u8 posX;
u8 posY;
u8 width;
u8 height;
void* callback_function;
struct Button* array;
u8 itemsInArray;
} Button;

Note:
The button-struct is not a very good struct itself, but it’s an example for this error.


Structs with function pointers with arguments

29/01/2010

Example: (complex, in principle only needed when you dive into C and are interested in pointers and void pointers)

http://cid-58814201e79976dd.skydrive.live.com/self.aspx/iepro/functionPointerExample.zip

This example has an example of making an array of objects (buttons) having as members function pointers.

These function pointers take arguments being  another member of the struct.

setting the pointers and its argument:

void setFunctionPointer_buttonView( void* myFunction, BUTTONVIEW* myButtonViewPtr, float myArgument ){
void (*foo)(float);
foo = myFunction;
((BUTTONVIEW*) myButtonViewPtr)->pushedButtonFunction = foo;
((BUTTONVIEW*) myButtonViewPtr)->argumentOfPushedButtonFunction = myArgument;
}

example of calling the function pointed at when clicked within the button:

void clickedWithinButton_buttonView( u8 clickPosX, u8 clickPosY, BUTTONVIEW* myButtonViewPtr ){

u8 positionX = myButtonViewPtr->posX;
u8 positionY = myButtonViewPtr->posY;
u8 _width = myButtonViewPtr->width;
u8 _height = myButtonViewPtr->height;

u8 left =   positionX;
u8 right =  positionX +  _width;
u8 up =   positionY;
u8 down =   positionY + _height;

if ( ((clickPosX > left) && (clickPosX < right)) && ((clickPosY > up) && (clickPosY < down)) ){
//playDrop() ;
void (*foo)(float);
foo = myButtonViewPtr->pushedButtonFunction;
float myANgle =  myButtonViewPtr->argumentOfPushedButtonFunction;
foo(myANgle);
}
}


Weird compiler problem

22/01/2010

In my game the player can move blocks in four directions. While developing I used a printf() above the code that is causing problems. When I remove this printf I can no longer move boxes up, but I can move them in other directions.

I’ve also removed the printf() and called a different function. Same result, even if the function does nothing.

On the NDS it worked the same as in de emulator.

I compiled it using Eclipse and it turned out it does work when compiled in Programmers Notepad. So it’s appears to be a compile problem from Eclipse.

See images below:


C Compiles backwards

22/01/2010

Now a little post about C that apparently compiles backwards.

If you include some files into your project, the file that is included last can’t make use of the file you included first.

Example of how I experienced this habit of C:

main.c
#include “prep.h”
#include “run.h”

… some simple code here, cause I’ve written it myself …

prep.h
#include <nds.h>
#include “draw.h”
#include “button.h”

button.h
… do some things with u8 and u 16 …

This will result in the following error:
“error: expected declaration specifiers or ‘…’ before ‘u8′”

Because button.h needs nds.h, but it only is included in prep.h, apparently after button.h is compiled.
So you can remove the include of nds.h from prep.h and put it in a file that is the tiniest brick of your project! In my case I can inlude it in button.h and remove it from prep.h and main.c. Button.h with nds.h is compiled first, so prep and main can make use of the stuff of nds.h.

Prettig weekend !


Game reaches gold-status

19/01/2010

A few weeks ago I had no knowledge of C or NDS programming and now I’ve finished my first game.

Game concept
The idea for this game comes from an old game of which I can’t remember the name. I played this game a lot when I was about 8 years old. And now I had to make a game my self. So I took the main concept from this old game and changed it a bit.

The idea is you have to collect points, and make your way through rooms by moving boxes.

Room number oneRoom six

Programming
I had no experience with C or programming without OO, so it took some time getting used to C.

I started building a lot of things I’m used from Java, like the panels, the draw functions and the actionlisteners, but after some time it got either to big to understand or the NDS emulator couldn’t handle it. But most of it did work and I learned a lot from it.

About two weeks ago I made a clean start and only made the things needed for my game. I made the map I was going to use in Numbers, made the images and made sure I could draw them on the screen, I made the structure for the data, and made the functions to listen to the buttons.

After this it took a few hours of programming and debugging, and after that I only needed to add some extra’s like the game-over screen and the sounds, but the main concept was working.

The game is now complete. Most of my ideas are in it and I can’t find anymore bugs. Comments are welcome, and here’s the game with source files: download

Problems
I had no experience with C, so I had some problems getting used to this way of programming. Especially in the beginning when I made it to complicated.

I had some problems with pointers to typedef structs, but in the final game I haven’t used pointers or typedef structs. I also had problems with the sounds but this was because I used the wrong makefile.

Overall I’ve learned a lot, but for my next game I’d prefer a OO-language and I’d propably choose a different platform.