gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Quick question

#14241 - DDRfreak121 - Sat Jan 03, 2004 3:33 am

I was wondering if you could instead of typing out code for sprites and such everytime.....make a header file containig the functions then just put

for example blah();

in the main code

Would that work and if so where can I find a tutorial to help me out a little bit

#14242 - zazery - Sat Jan 03, 2004 3:53 am

That is exactly what people do. They write functions to save time. I'm not the best at explaining them maybe but I know how they work.

In simple terms, you put numbers in it does stuff and can give you a result if you want. You should read up on functions if you haven't already.

Game engines make it very simple to create games and that's what all big games use. HAM for the GBA is a good example of what I mean. I believe that is what you are looking for. Eithier get that or create your own functions to use. Currently I am creating my own engine in use for my game. I believe someone had some header files that included most of the functions you would need to include. I can't seem to find it but hopefully someone can point you to them.

From what I think, you may not know programming too well, especially functions. Try simple C++ with the PC first to understand functions better.

Hope this post helps clear up what you were wanting to know.

#14244 - DDRfreak121 - Sat Jan 03, 2004 3:59 am

I know functions....Im just not sure how to set them up in this case

#14245 - XeroxBoy - Sat Jan 03, 2004 4:06 am

Code:
// header.h


// function declaration - return type goes at the beginning
// a list of arguments goes next in the brackets

int Blah(int x);

int Blah(int x)
{
        //your code goes here
        return 0;
}


Code:
//main.c

//various includes
#include "header.h"

void AGBMain()
{
        // your code
        Blah();
}


That's the gist of it, anyways. But seriously, this is elementary stuff - you really should read some books/tutorials - the ones at www.cprogramming.com are a good place to start.

#14247 - DDRfreak121 - Sat Jan 03, 2004 4:44 am

I have read books...I was just making sure on it

#14252 - DDRfreak121 - Sat Jan 03, 2004 5:53 am

Heres what I did

header
Code:
#include "gba.h"       
#include "dispcnt.h"
#include "nps.h"


int image(int x);

int image(int x)
{

void PlotPixel(int x,int y, unsigned short int c)
{
   VideoBuffer[(y) * 120 + (x)] = (c);
}

int main()
{
   int loop;
   int x, y;

        SetMode(MODE_4 | BG2_ENABLE); //set mode 4 and enable background 2

   for(loop = 0; loop < 256; loop++)                 //256 entries allowed
      BGPaletteMem[loop] = npsPalette[loop]; //load the palette into palette memory

   while(1)
   {
      for(y = 0; y < 160; y++)                  //screen height
      {
         for(x = 0; x < 120; x++)          //screen width
         {
            PlotPixel(x,y, npsData[y*120+x]);   //load image data into
         }                  //memory pixel by pixel
      }
   }
}
return 0;
}



then heres the main code



Code:
#include "gba.h"
#include "dispcnt.h"
#include "nps.h"
#include "image.h"
void PlotPixel(int x,int y, unsigned short int c)
{
image();
}


error.....damn

Code:
C:\Documents and Settings\Zack1\Desktop\image library attempt>path=C:\devkitadv\
bin

C:\Documents and Settings\Zack1\Desktop\image library attempt>gcc -o main.elf ma
in.cpp -lm
In file included from main.cpp:3:
nps.h:1957:70: warning: no newline at end of file
In file included from image.h:3,
                 from main.cpp:4:
nps.h:9: redefinition of `const u16 npsData[]'
nps.h:9: `const u16 npsData[19200]' previously defined here
nps.h:1931: redefinition of `const u16 npsPalette[]'
nps.h:1931: `const u16 npsPalette[256]' previously defined here
In file included from image.h:3,
                 from main.cpp:4:
nps.h:1957:70: warning: no newline at end of file
In file included from main.cpp:4:
image.h: In function `int image(int)':
image.h:12: syntax error before `{' token
image.h: In function `int main()':
image.h:32: `PlotPixel' undeclared (first use this function)
image.h:32: (Each undeclared identifier is reported only once for each function

   it appears in.)
image.h: At global scope:
image.h:37: syntax error before `return'
main.cpp: In function `void PlotPixel(int, int, short unsigned int)':
main.cpp:6: `void PlotPixel(int, int, short unsigned int)' used prior to
   declaration
image.h:9: too few arguments to function `int image(int)'
main.cpp:7: at this point in file

C:\Documents and Settings\Zack1\Desktop\image library attempt>objcopy -O binary
main.elf main.gba
objcopy: main.elf: No such file or directory

C:\Documents and Settings\Zack1\Desktop\image library attempt>pause
Press any key to continue . . .


Im sorry.....I just cant figure this out

#14253 - sajiimori - Sat Jan 03, 2004 6:09 am

Seems like you need a good book on C. I recommend The C Primer. You can probably get people to give you code to solve your problems (people here are nice that way), but you won't learn very much.

Somebody will probably come by and post some correct code for you, but the reason I'm not doing that is because your understanding of C is not complete enough for such code to help you in the long run.

Good luck! :-)

#14254 - sajiimori - Sat Jan 03, 2004 6:23 am

BTW XeroxBoy, be careful to not post misleading information. Your example shows code appearing in header files, which is truly a bad idea.

One last comment, directed at everybody who reads this: It's ok to not know something. Insisting that you already know is a hinderance to progress. I've been programming for years, but I am still learning -- my primary reading material is about programming.

The people who insist that they know everything are the most ignorant of all.

#14271 - DDRfreak121 - Sat Jan 03, 2004 4:57 pm

GAHHHH

Ive read all ,my C books and looked up functions online

For some reason they dont make sense

could smeone please just help me...if I run into another problem Im locking myself away for 3 monthes just to read them over and over

#14274 - poslundc - Sat Jan 03, 2004 6:54 pm

If you do not understand how a function works, then you need to work through any of a million C tutorials. Look for ones that teach with examples. Then take the examples and code them in yourself (don't just copy/paste, force yourself to retype them in). Reading about them in books is all well-and-good, but if you're confused then it's time to start learning by doing. Functions will make a LOT more sense once you've actually programmed a few successfully.

For the record, the code that you've posted is WAY off-base, and anyone here simply correcting it for you would not be doing you any service by it. GBA programming is tough for people who already have a firm grasp of C programming, and you do not have that yet.

I'm not trying to discourage you from GBA programming; if you work at it and are a quick study then it could take you as little as a day to get the necessary experience programming in C to start tackling the GBA. But if you attempt to circumvent the learning C step, then I do not think you will not progress very far.

Good luck,

Dan.

#14275 - yaustar - Sat Jan 03, 2004 7:10 pm

Quick answer, you have the 'main()' function in a header file when it shoulkd be in the main source file.

eg
header file
Code:

plotpixel(int x, int y)
{
 blah balha hahdsajkd
}

main scurce
Code:

#include "bhbhbhbh"
main()
{
 blah
 while(1)
 {
   plotpixel(10,10);
 }
}

very quick example.

Try using consle programming and call a function that simply couts or printfs a line to the screen first then use the same princable on the GBA.

www.gametutorials.com

edit: code should be put into source files as sajoimi (sorry, cant spell your name) said as:
Code:

//bg.h
void something(void);

Code:

//bg.c
#include bg.h
void something(void)
{
 do something
}

then in main source include the header file.
For the moment you can get away with putting in a header file but in general it is very bad practice.

I can and will correct it for you if you ask and really feel like you hit a brick wall but as poslandc said, it really isnt going to do many favours later.
_________________
[Blog] [Portfolio]

#14278 - DDRfreak121 - Sat Jan 03, 2004 7:53 pm

Im going to do the C and C++lessons at gametutorials website

thanks

#14279 - plasma - Sat Jan 03, 2004 8:14 pm

Another thing, you can't write a function inside a function - e.g. it won't compile if you write main() inside image()

#14291 - Miked0801 - Sat Jan 03, 2004 10:53 pm

That brought back memories of my PASCAL days - functions in functions :)

#14300 - dagamer34 - Sun Jan 04, 2004 1:09 am

The GBA Appwizard has some headers that serve as a very good base for your code.

If all else fails and you run out of ideas for functions, just look at HAM, it has TONS. Of course, don't copy!
_________________
Little kids and Playstation 2's don't mix. :(

#14303 - yaustar - Sun Jan 04, 2004 1:55 am

I suppose the quickest example would be gbajunkie's bg.h and bg.cpp files in chapter 5 or 6.
_________________
[Blog] [Portfolio]