Guest User

Just another simple GBDK tutorial(by VKcorp)

a guest
Sep 12th, 2010
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. Disclaimer
  2. All here is for educational purpose and shoud be used at your own risk!!! GAMEBOY(c) from NINTENDO(r)
  3. Just onother simple GBDK tutorial(by VKcorp)
  7. PART0: Knowledge needed(done)
  8. PART1: What do you need(done)
  9. PART2: Installing gbdk(done)
  10. PART3: The programs(done)
  11. PART4: A little bit of C(done)
  12. PART5: Coding1 variables + basic instructions(done)
  13. PART6: Makefile(copmpiling)( done)
  14. PART7: Sprites(not done)
  15. PART8:TKS(done)
  18. Part0: Knowledge needed
  20. Common sense + how to use your OS and browser. Know that a GB is a GameBoy.
  23. Part1: What do you need??
  25. Force of Will
  26. A version of GBDK(gameboy dev kit)
  27. Tile Editor
  28. GB Tool Kit and a Map Builder(not needed for this tutorial)
  29. Notepad, vi, or any text editor
  30. An emulator
  31. You can get all this stuff + more documentation on:
  35. Part2: Installing GBDK
  37. On windows:
  38. -Unzip all to c:\
  39. -Run: c:\SDK\gb-gb\x.x.x\lib\make.bat
  40. -Done
  41. On *nix:
  42. -Decompress GBDK to a folder
  43. -Get a way to be root(su root), if you need get john the ripper to crack the password, if you get cought say "it was for educational purposes"
  44. -run: make
  45. -run: make install
  46. -Done
  49. Part3: The programs
  51. GBDK
  52. -On the bins you will find:
  53. -as: assembler
  54. -cpp: c++ compiler
  55. -lcc: the binary we're gonna use
  56. -link: linker(joins object(compiled) files)
  57. -Includes: are files that you will need to make any use of the compiler, unless you write your own. They are headers wich you insert when you begin your source ex #include <gb.h>
  59. Tile Editor
  60. -The tile editor is pretty easy to use, just when you export files you goto: export to..., and change the type to GBDK C file. NOTE: if you are exporting more than 1 sprite, dont forget to change the To: textbox. Also you can change the label to any name like Retard_Head but dont use spaces, or... YOU DIE!
  61. <no pics in plain text buddy>
  63. About the emulator: read the documentation that comes with it.
  66. PART4: A Little bit of C
  67. Some terms to learn:
  68. The four things computers and any useful machines have: input, output, storage, and logic. Input/Output: Immediately you can recognize that the GB has input through it's buttons. No input would be complete without some way of acknowledging it, output. In the case of our GB, we have a LCD screen capable of displaying 4 colors. Input/output seem to go hand in hand wherever you look. When something goes in, something comes out. In lew of this, the two things are usually globbed together under one term: "i/o", an abbreviation for input/output. Storage: Temporary and Permanent: We know that the GB has a i/o, but where does it keep the game data? The game screen seen is simply a display of the data where the GB keeps the shows whast um on the video ram(VRAM). Internally the GB has some place to store this data. This is, obviously, known as storage. When you turn off your gb its all gone. This means the data for the video is in temporary storage(RAM). In computery terms, our version of temporary storage might be defined as data storage which is reset, blanked, or invalid when necessary power is lost or shut off. Sounds complex, but just think: you turn of your gb. All temporary storage is gone. Than you take you gamepack out and turn the GB on and tadah!! The GAMEBOY logo apears! How, it was stored in the permanent storage(ROM) in this case the gameboy bios, that data was than copyed to the VRAM so that you could see it!!
  69. Any given storage, regardless of permanence, will have one of two different access privileges: read-only or read/write (I've never heard of write-only, but it if you create it you wont get rich). Read-only can never be changed while read/write storage can. It's that simple. And lastly the concept of storage could be described simply in terms of i/o from a storage machine/device. Say your GB storage is a little chip inside the GB. Reading data from that storage would be output from that chip received as input; and vice versa when writing.
  71. Logic: our machine can access input, display output, and keep a game screen in it's storage; but how does it bring all these things together and control them? Logic. You can think of logic as instructions that encompass reading input, writing output, and making decisions.
  72. Lets look at the logic for a supposed car game.
  73. If in the START button on the main screen is pressed start a new race.
  74. If you press left or right your car steers.
  75. If the A button is pressed, increase the speed by 2 every second and update the speedometer.
  76. If the B button is pressed, use a nitro and take one nitro out of your remaining ones.
  77. If your gas tank was filled, update the gas display.
  78. If the gallons of gas are smaller or equal to zero, stop the car.
  79. Easy huh!!??
  81. Identifiers(functions): would be like the names you use in spoken language. The character value of the name itself means nothing(like Joe doesnt have a meaning like table), it's who the name refers to that's important. An identifier basically a name or made up of names, just like you probably(unless your name is table) have a last and a first name. These identifiers act just like names because they "identify" something other than the name itself. An identifier is simply a label for data and/or functionality. Some very common identifyers are: main(), printf(), if() and many others. As said you can make up you identifyers:
  82. #include <stdio.h>
  83. myfunction(int q){
  84. if(q<10)printf("%d is less then 10",q);
  85. }
  86. main(){
  87. myfunction(5);
  88. }
  89. The output is:
  90. 5 is less then 10
  91. The function myfuncion() tests if q is less then then and then, if so is prints it.
  93. Operators: its simple: no oparators no logic. Operators are very important in C as in other languages. They compare, modify and set variables and constants. The operands are:
  95. Equals = assigns
  96. Plus + add
  97. Minus - subtract
  98. Asterisk * Multiply
  99. Slash / Divides
  100. Comparisson equals == Compares if 2 values and/or variables/constants are the same
  101. Less then < Compares if A is less then B
  102. Greater then > Compares if A is greater then B
  103. Less then or equals <= Compares if A is less or equals to B
  104. Greanter then or equals <= Compares if A is less or equals to B
  105. Not equals to != Compares if A is not equal to B
  106. Double crazy e && AND logical
  107. Double vertical bar || OR logical
  110. The smallest C program: The smallest prgram you can write in C is:
  112. main(){}
  113. The output would be nothing.
  115. The compiler wont work if you dont have this inside your code. main() is the main function needed for your program. Is there where the compiler will put the other functions to work.
  117. Variable: exactly like in Maths, something that changes, varys. A variable in C is a placeholder for a value. A variable is used to store information used by the program. When you define a variable in your program, you tell the compiler how much memory to set aside Based on the type of variable, the compiler sets aside the required number of bytes of memory. In C there a several types of variables int(integers), char(ASCII characters), void(null), float(real numbers), double(large real numbers).
  119. Comments: in C if you want to put a special comment like a disclaimer or some hints one your source code you can do it with /**/ or //. Here is how:
  121. /* comment here
  122. saf dafds
  123. fsdaf
  124. */
  125. printf()("hi"); //this prints hi, but this comments wont be printed
  126. /*
  127. *this is
  128. *a legal
  129. *comment
  130. *very used in this format
  131. */
  133. Constants: they are also data storage locations. However the value of a constant remains the same throughout the whole program and you cannot set another value to a constant once it has been defined with a value. A constant can be defined by using #define name_of_the_constant value
  135. Includes: in C as in pretty much every other programming lenguages includes are very important. Includes are the insertion of other files in the header of your code, those files can contain basic functions like main() or if() or it can contain some other thing like the image maps for you game. You insert a include in a code with #include <filename.h> if you want for the compiler to look for the file in the default include folder(include/) or you change the <> for " " if you want the compiler to look for the include file in the same folder as your source code is.
  137. Loops:
  138. The While Loop: A while loop causes the set of statements within the loop to repeat again and again as long as the starting condition is true. The usage for the while loop is as follows:
  140. while (condition) //like while(a<10) it mean while a is less then 10 repat
  141. {
  142. statements;
  143. }
  145. The For Loop: A for loop is used for iteration of a set of statements like the while loop but it combines three different operations into one statement: initializing the index(a variable), checking for termination of loop, and incrementing the index. The usage for the for loop is:
  147. for (initialize index; test condition; increment index) /* like for(a=0;a<10;a++) it means a starts at 0 and while a is less then 10 a will be increased by 1 and the statements will be repeated*/
  148. {
  149. statements;
  150. }
  152. The index is initializd to a starting value. The statements within the loop are executed as long as the test condition evaluates to true. The test condition can be any valid C expression. The index is incremented after each iteration of the loop. Note that the increment index actually can map to any legal statement though it is mostly used in the way shown above.
  155. About the structure: You can write a whole C program in one line as the ; is what determines the end of a like:
  157. #include <stdio.h>
  158. main(){printf("Hello");printf("World");printf("IM HERE");}
  160. is completely accepted by a c compiler as:
  162. #include <stdio.h> //add the standard i/o header to your code
  163. main(void) //starts the main part of the code
  164. {
  165. printf("Hello World IM HERE");
  166. /*this up there will cause the GB to output whats between the quotes to the LCD*/
  167. /*to compile this source create a new file named hw.c and insert this lines in the file then save it in \sdk\gb-gb\x-x-x\bin. Run: lcc hw.c -o have fun:)*/
  168. }
  169. The outputs for both will be:
  170. Hello World IM HERE
  172. the code above also is. All functions in C MUST contain () like main(); or cls();. In those 2 exemples the value requered beteween the parentesis is NONE or void wich is a type of variable that indicates 0. This is not accepted:
  174. #include <stdio.h>
  175. ma
  176. in(void)
  177. {
  178. pri
  179. ntf("Hell
  180. o World");
  181. }
  183. A instruction gotta be placed in the same line, unless your space on the line ends like:
  185. printf("fakdkljsdglkfdlasfjhlkghlhyrtiuyknm,fhckajhvmdfhyui fjhgkl hfkh k lfhk hklhk lhkljfdh klh kjlh sdkh klyfiu yewih kjheryi hkh kdshsfadhjhdskhfdlskhfakljhk lhkl hklhd khk hkhsdkhdfhkj kjh kjhkhf khd khkjf hkjhk hkjhd hkjdf hkh fkjhkj kdheiywriyrrh");
  187. Thats completely legal if you dont hit return since it would still be seen as a whole single line if you dont have word warp active. Also if you write 2 printf in separate lines the will still be in the same line, you have to use \n to skip lines:
  188. main(){printf(){"Hello\nWorld");}
  190. PART5: Coding1 variables + basic indentifyers
  192. Here is the commented source of a gb program that slowly prints a nice Hey Fuck You. Remember that the makefile will work only that comes with this zip file if you put it on a subdir on the BIN folder of you SBDK.
  194. /*
  195. *Open This in Full Window with no word wrap
  196. *This is a source was created by Vinicius Kursancew
  197. *Feel free to modify and distribute
  198. *This software has no warranty and dont blame me if your computer blows up while you use it
  199. */
  200. #include <gb.h> //include the Gameboy header with some specific identifyers
  201. ps(char ch){ //starts the ps custom identifyer, wich needs data input between the ()
  202. printf("%c",ch); //prints to the screen the value that ch contains
  203. delay(150); //waits for 0.3 seconds and proceed you can change it if you want
  204. } //ends this function
  205. main(){ //start of the main section of the program
  206. int rep; //defines a integer: rep
  207. disable_interrupts(); //turns off teh interrupts
  208. for(rep=0;rep<10;rep++){ //start the for loop: rep starts at 0, when the structure is done ad 1 to rep and if rep is not less then 10 stop and proceed with the rest of the program, it means that 9 lines will be printed
  209. printf("\n"); //prints a blank line
  210. ps('H'); //now we are using our function, it will print H(always pu between ' ' when a single character
  211. ps('e');
  212. ps('y');
  213. ps('!');
  214. ps('F');
  215. ps('u');
  216. ps('c');
  217. ps('k');
  218. ps(''); //Here a blank space is inserted
  219. ps('Y');
  220. ps('o');
  221. ps('u');
  222. ps('!');
  223. } //ends the for loop
  224. delay(2000); //waits for 2 seconds
  225. reset(); //resets the gameboy
  226. } //ends the main function
  228. Save this source in the SDD\gb-gb\x-x-x\bin\tut1\fuck.c (if you are using notepad dont forget to take .txt out)
  231. PART6: MAKEFILE(compiling)
  233. OK we got the source, now we have to compile, great! We will make a make.bat file. Open your favourite editor. Copy this to it:
  235. ..\lcc -Wa-l -c -o fuck.o fuck.c
  236. ..\lcc -Wl-m -Wl-yp0x143=0x80 -o fuck.o
  237. Saving if for DOS/Windows users.
  238. -Save it and move it to make.bat
  239. -run it
  240. For *nix users:
  241. -Save it as mk
  242. -change the attributes to 4775(chmod 4775 mk)
  243. -run it
  247. Yes lets put people on gameboy! Remember that the makefile will work only that comes with this zip file if you put it on a subdir on the BIN folder of you SBDK
  249. /*
  250. * in this source you will(maybe) learn how to place a background, a sprite and move the sprite around
  251. * use it at your own risk
  252. */
  253. #include <gb.h> //include the file gb.h to the header
  254. #include "i.h" //include our images to the header(its in this zipfile)
  257. int main(void){ //starts the main function
  258. int x,y,st,key,z; //define th integers x and y wich will be used to define the pacman position, key will be used later
  259. x=20; //set x as 20
  260. y=20; //set y as 20
  261. st=0; //sprite type(1=closed 0=open)
  262. z=0; //set z=0, z will be used for testing if its time to change sprite or not
  265. set_bkg_data(0,0,back); //set our backgroud data(1st tile, number of tiles, name of the bkg)
  266. set_sprite_data(0,3,sprt); //set our sprite data
  267. wait_vbl_done(); //wait for the visual blank to be done
  268. set_sprite_tile(0,0); //set our tile 0 as the tile 0 of our tile data-pacman open
  269. set_sprite_tile(1,1); //set our tile 1 as the 1st tile of our tile data-pacman closed
  270. SHOW_BKG; //display the background
  271. move_sprite(st,x,y); //place the sprite 0 in 20,20
  274. move_sprite(1,200,200); //place sprite 1 off the screen
  275. SHOW_SPRITES; //display the sprites
  276. key=0; //set key=0, key will be used to store joystick data
  277. while(!0){ //repeat the while structure forever
  278. key=joypad(); //set key with the current joypad value(Button up, down, A...)
  279. if(key==J_UP){ //if the up pad is pressed
  280. y-=1; //reduce y by one
  281. move_sprite(st,x,y); //move current sprite 1 point up
  282. }
  283. key=joypad(); //set key with the current joypad value(Button up, down, A...)
  284. if(key==J_DOWN){ //if the down pad is pressed
  285. y++;
  286. move_sprite(st,x,y); //move current sprite 1 point down
  287. }
  288. key=joypad(); //set key with the current joypad value(Button up, down, A...)
  289. if(key==J_LEFT){ //if the left pad is pressed
  290. x-=1;
  291. move_sprite(st,x,y); //move current sprite 1 point left
  292. set_sprite_prop(0,S_FLIPX); //flip sprite
  293. set_sprite_prop(1,S_FLIPX); //flip sprite
  294. }
  295. key=joypad(); //set key with the current joypad value(Button up, down, A...)
  296. if(key==J_RIGHT){ //if the right pad is pressed
  297. x++;
  298. move_sprite(st,x,y); //move current sprite 1 point right
  299. set_sprite_prop(0,0x00); //unfilp sprite
  300. set_sprite_prop(1,0x00); //unflip sprite
  301. }
  302. delay(20); //wait 20 ms
  303. z+=1; //add 1 to z
  304. if(z==5){ //if z is equals to 10
  305. move_sprite(st,200,200); //move current pacman out of screen
  306. if(st==0){ //if pacman is open now is closed
  307. st=1;
  308. move_sprite(1,x,y);
  309. z=0;
  310. goto jmp;
  311. }
  312. if(st==1){ //if pacman is closed now is open
  313. move_sprite(0,x,y);
  314. st=0;
  315. jmp:
  316. z=0;
  317. }
  318. }
  319. }
  320. reset(); //reset the GB
  321. }
  323. PART8: TKS
  324. There isnt a lot to put here i pretty much learned C by reading books in my library... thanks to H. Mulder tools, Jeff's, Christopher W. Fraser and David R. Hanson.
  326. Me:
  327. V. Kursancew, born 8/25/85
  328. Questions about THIS TUTORIAL:
  329. UIN 43437330 or PeDrEiRo- on EFnet
  330. This doc was written in July/2001
Add Comment
Please, Sign In to add comment