Community game developing culture and challenging competitions


mjohansson

Supporter
Joined
Feb 10, 2011
Messages
409
Hey, looking at C64 games now, thinking of programming for modern hardware and drawing graphics in "retro style", and the current Dragonbox competition, it all got me thinking.

How about specific game developing competitions, with rules like what max size it can be, like make a platformer in less then 2MB. Back in the day it was much harder to make games I reckon, cos of limited ram, storage and cpu frequency.

And couldnt this be cool to learn people programming? And make us all better by finding out trix and tweaks and stuff that might be usable for all sorts of other things.

With the pnd compilers its dead easy for someone who cant code to get some visual result up and running immidiatly and start tweaking the controls or physics and whatever of some allready written game code. 

Couldnt this build a developer community and increase interest in Pandora?

And any competitions should be organised and priced by the community, EvilDragon has enough to do and enough expenses already I think.

Pandora, the must have for any new game developer wannabe, with the excellent community of greatly skilled coders.

Atleast we should do challenges like severe restrictions and making a specefied game type genre with rules of what must consist of or something like that, it would just be so much fun, and shouldnt take to much time cos its mainly coding and only small in scope.
 
Hey, looking at C64 games now, thinking of programming for modern hardware and drawing graphics in "retro style", and the current Dragonbox competition, it all got me thinking.

With the pnd compilers its dead easy for someone who cant code to get some visual result up and running immidiatly and start tweaking the controls or physics and whatever of some allready written game code. 
Where would one find one of these pnd compilers? and I can't code my way out of a paper bag, so how easy can it be? I wouldn't mind learning, but not if it's TOO hard.

How about specific game developing competitions, with rules like what max size it can be, like make a platformer in less then 2MB. Back in the day it was much harder to make games I reckon, cos of limited ram, storage and cpu frequency.

And couldnt this be cool to learn people programming? And make us all better by finding out trix and tweaks and stuff that might be usable for all sorts of other things.
This is a really good idea. I'll admit to having something of a mild fetish for the old watch/game things, that were LCD. Yeah, they were real simplistic kinda games, but in their way they were a lot of fun...especially for an eight to ten year old...and this could help some of us recapture our youth, LOL.
 
Generally generating PNDs is not overly complicated for those with basic knowledge of compiling code from the source.. If you deal with Linux on a regular desktop you tend to learn to do this after a while. However when things don't compile properly that's when knowing how to code comes in handy and things sometimes go wrong with dealing with ARM based devices since a good majority of the code was programmed for regular x86 in mind.

Generally making a PND follows this processes..

- Compiling the source code.. Either directly on the Pandora via cdev or codeblocks PND or via a Cross Compiler environment(compile for the Pandora on your desktop) via Sebt3's Yactfeau or Ivanovic's  setup.. - If the program is Python which is a language that don't need to be compiled  or if it's a Java binary which may already compiled you just need to know the command to run it and not worry about compiling. 
- Setting up your Icons, preview screenshots, startup script(s), copying any other required files into the right place and creating a PXML file(xml file that holds all the Information to run the program, what icon, screen shots to use..etc)
- Packaging.. this bundles it all together into the PND file you know and love.. generally a command line tool in linux, but there is a Windows base tool that has a gui and such. 

It's a bit to learn how to do everything, but I think most people that can sit down and try things can figure it out over time. Knowing how to code can help make a game more optimized to the Pandora and such.
 
Last edited by a moderator:
There is so much confusion in the OP. :) Nice reply by TrashyMG


You don't need to make a PND to run a game on the pandora. You can run a normal linux game by opening a terminal, "cd" (change directory) to the /path/to/game, then typing ./yourgamename.


There are no PND compilers since PNDs are not executeable binaries.


The current dragonbox competition only has a handful of entries: restricting the type of games accepted even more would lead to a competition with even less entries. Restricting the size of the game to a few MB doesn't make it easy for people with no skills to make a game.


You are right that the easiest way to make a running game is to modify an existing game. But you still need to learn C or C++ http://www.google.com/m/search?q=beginners+guide+to+learning+c%2B%2B and a game library like SDL: http://www.lazyfoo.net/SDL_tutorials/


I gave links to C++ tutorials but I personally find C to be simpler and less abstract for beginning programmers. Learning about procedures, functions, data types and basic input/output first is oldschool, but imo effective.
 
Hummm...I tried to learn Virtual Basic and had absolute FITS. So I tried Virtual C++ and had FITS.

I couldn't make a damn database for the life of me.

I dunno. I would like to think coding for Pandy was somewhat easier than that. I have only experience in coding php and html
 
I didnt mean creation of pnd files, I mean the CDEV pnd for ex, compiling is the hardest part of beginning to learn programming and is never explained in detail. With the CDEV it would be easy to compile for Pandora, cos you dont need to configure or install anything yourself, you just download the pnd and your ready to go, it has SDL and GLES librarys already ready for use.

But its a good point that limits like a small size for ex would make it difficult to learn from, it has never been easier to learn to make games because of powerfull hardware and easy to use librarys like openGL, the restriction idea was to mimic the conditions of past hardware to some extent just for fun development challenges and potentially self education or education by looking at what other did in same situation.

But I should take things a step further independently, and then mayby people will join as time goes by and something interesting becomes of it, IF something interesting becomes of it. So Im gonna make some test code, we had problems with what was the community game project before with parralax layers, I made sort of a software renderer that ended up being ridicolous slow, but I wonder if something can be made with a similiar idea, cos if you look at old games they often have flashing colors, that could be easily achieved by for ex passing a pixels R channel + 0.3 * time_value, if the variable is limited to 1.0 scale or if its in char data format for 256 values? But in SDL you dont do that, you just send a complete image, you need to find and modify the right spot in memory. Manipulating images excites me, I need to look into this further.

I should also write my own beginners programming guide, it may not be a good guide, but Im sure it will be slightly different from anything Ive yet seen and because of that help someone who didnt pick up on more standard guides, for ex I only know of one single guide Ive seen that indirectly explained that variables were actually RAM, for someone who wants to write to memory to create graphics starting to talk about variables was atleast hopelessly confusing to me.

For a beginner game developer you want graphics on the screen from the very beginning, it has been the best way for me to learn cos you can make visual test and get visual feedback to your tests, so you get much better clues to what is going on, theres also a lot of math and things wich is essential to game developers that are poorly explained in all guides, it seem all guides think people want high level coding style to make databases or webpages and thats it? Well theres gotta be more people like me that want to manipulate the hardware themself on the lowest level. Wich reminds me, its time to start looking at assembly now! :)

Ill start with the guide thingy, I got really excited about this now!
 
If you do publish a guide, please try to make it in layman's Engliah, and minimize the amount of tech-talk...make it truly a guide for dummies like me.
 
I started working on the guide, I didnt realise just how little I actually know about coding :eek: !

I was gonna make source code repeating errors Ive done in the past only to get a different result now!! :eek:

I also realised while loop is a function... and that variables declared inside a loop have a scope limited to inside that function... 

Also I had major problems making a program that in the end achieved what I wanted but not in the way I wanted it, perhaps some one here would know? Can you not call SDL_GetTicks(); several times close to eachother? I froze the program at that, I need some specific testing on that later I guess...

Its ridicolous that I should write a guide being of so little knowledge as I am, but none the less I am completly convinced I can make a good one to get anyone started with game develompment. Yea, this will end up a truly magnificently special kind of guide for sure!! :D

I think Ive gotten the perfect idea for how to do this. Ill get through tomorrow I think. Check back Sunday night or Monday.
 
Ok, this I can not figure out... The code works like this, but unblank either of the tv4 assignment lines and the program freezes, WHY??!! I tried putting it in different places, adding delay, but I tested a seperate file without this content to access SDL_GetTicks a billion times one after another into different variables and there was no problem whatso ever. So why cant I access SDL_GetTicks here, I mean I put the command straight above currentTime=SDL_GetTicks(); and it still freezes :eek: ??? WTF?!!

 



Code:
#include "iostream"
#include "stdlib.h"

#include "time.h"

#include "SDL/SDL.h"

 

using namespace std;

int main(){

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);

SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE|SDL_NOFRAME);

SDL_Event game;

SDL_Rect temp;

Uint8* key;

int quit=0;

Uint8 r=255,g=255,b=255;

int currentTime,oldTime,framerate = 10;

 

 

  int xp=80,yp=80,tv1,tv2,tv3;

  int tv4;

  int change_color=0,change_color_pressed=0;

  

while (quit==0){

 

  while (SDL_PollEvent(&game)){

if(game.type==SDL_QUIT){quit=1;}}

 

   key=SDL_GetKeyState(NULL);

   if(key[SDLK_ESCAPE]){quit=1;}

   if(key[SDLK_a]){change_color=1;}else{change_color_pressed=0;}

   if(key[SDLK_UP]){yp-=1;}

   if(key[SDLK_DOWN]){yp+=1;}

   if(key[SDLK_LEFT]){xp-=1;}

   if(key[SDLK_RIGHT]){xp+=1;}

   

   if(xp<0){xp=0;}

   if(xp>200-40){xp=200-40;}

   if(yp<0){yp=0;}

   if(yp>200-40){yp=200-40;}

 

   tv1=currentTime;

   //tv4=time(NULL);

   //tv4=SDL_GetTicks();

   tv2=oldTime;

   tv3=(tv1*tv2)/90909;

   

   if(change_color==1&&change_color_pressed==0){

    srand(tv1);

    r=rand()%255;

    srand(tv2);

    g=rand()%255;

    srand(tv3);

    b=rand()%255;

    cout<<"R = "<<(int)r<<", G = "<<(int)g<<", B = "<<(int)b<<endl;

   change_color_pressed=1;}change_color=0;

   

temp.x=0;

temp.y=0;

temp.w=200;

temp.h=200;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,0,0));// CLEAN THE SCREEN.

 

temp.x=xp;

temp.y=yp;

temp.w=40;

temp.h=40;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,r,g, B) );

 

 

SDL_Flip(dis);

 

currentTime=SDL_GetTicks();

if(currentTime-oldTime<framerate)

{SDL_Delay(framerate-(currentTime-oldTime));}

oldTime=SDL_GetTicks();}

 

SDL_Quit();

return 0;

}



why do I see <div> all over the place, did I do something wrong? is this code possessed by a poltergeist of fuckup? This is scary...

#include "iostream"

#include "stdlib.h"

#include "time.h"

#include "SDL/SDL.h"

 

using namespace std;

int main(){

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);

SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE|SDL_NOFRAME);

SDL_Event game;

SDL_Rect temp;

Uint8* key;

int quit=0;

Uint8 r=255,g=255,b=255;

int currentTime,oldTime,framerate = 10;

 

 

  int xp=80,yp=80,tv1,tv2,tv3;

  int tv4;

  int change_color=0,change_color_pressed=0;

  

while (quit==0){

 

  while (SDL_PollEvent(&game)){

if(game.type==SDL_QUIT){quit=1;}}

 

   key=SDL_GetKeyState(NULL);

   if(key[sDLK_ESCAPE]){quit=1;}

   if(key[sDLK_a]){change_color=1;}else{change_color_pressed=0;}

   if(key[sDLK_UP]){yp-=1;}

   if(key[sDLK_DOWN]){yp+=1;}

   if(key[sDLK_LEFT]){xp-=1;}

   if(key[sDLK_RIGHT]){xp+=1;}

   

   if(xp<0){xp=0;}

   if(xp>200-40){xp=200-40;}

   if(yp<0){yp=0;}

   if(yp>200-40){yp=200-40;}

 

   tv1=currentTime;

   //tv4=time(NULL);

   //tv4=SDL_GetTicks();

   tv2=oldTime;

   tv3=(tv1*tv2)/90909;

   

   if(change_color==1&&change_color_pressed==0){

    srand(tv1);

    r=rand()%255;

    srand(tv2);

    g=rand()%255;

    srand(tv3);

    b=rand()%255;

    cout<<"R = "<<(int)r<<", G = "<<(int)g<<", B = "<<(int)b<<endl;

   change_color_pressed=1;}change_color=0;

   

temp.x=0;

temp.y=0;

temp.w=200;

temp.h=200;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,0,0));// CLEAN THE SCREEN.

 

temp.x=xp;

temp.y=yp;

temp.w=40;

temp.h=40;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,r,g, B) );

 

 

SDL_Flip(dis);

 

currentTime=SDL_GetTicks();

if(currentTime-oldTime<framerate)

{SDL_Delay(framerate-(currentTime-oldTime));}

oldTime=SDL_GetTicks();}

 

SDL_Quit();

return 0;

}
 
Ok, unpack the zip file and put it on your SD card, download CDEVTOOLS pnd and start it once, enter your system password, then type "exit" to exit program. Now put the hw.cpp file in the pandora/menu/appdata/cdevtools/mysrc folder.

 

And now follows the introduction I made so far, the thing is, its not exactly a miracle of goodness... I have the aspects that I thaught was important in the beginning, an overall idea of how it works with the hardware and so, but explaining how all coding works and why isnt easy, especially not when you dont really know it yourself! So do go to the official C++ webpages and look at the tutorials and references for a more accurate description and perhaps alltogether easier to understand explenation. I will make a part 2 tommorrow with some 2D gaming guide of some sort, if anyone try to make sense of this then please give some feedback if it was of any help at all or just terrible and causing more harm then good.

 

##############################################################################

 

When you start a program the entire program will load into RAM, the CPU then fetches one instruction after another. 

If the game is 7 gigabytes and you only have 256 megabyte RAM, the program in its entirety will most likely 

fit inside RAM all at once still, even modern games with millions of lines of code dosnt take up many 

megabytes of space, all those gigabytes of total space consists of media such as images and sound. 

When you use images and sound in the game they to have to be loaded into RAM for the CPU to be able to access them. 

Thats why you have loading times between levels, as new images for a new level loads to RAM in place of the 

old levels images.

 

A computer consists of different parts connected together on a motherboard. All programs loads to the RAM from 

whatever media they were originally stored on, then they tell the CPU if they wish to send data to the 

graphics card for example and the CPU will send the data through a path with the adress used for the GPU. 

This can be done manually with machine code, but this is not the guide to teach you that.

 

A CPU only deals with binary numbers, 0 and 1. C++ is a language that can be translated to binary data. 

Every instruction has its own combination of 0's and 1's. Because of that its possible to define 

how a specific word should be used in place or translated to specific binary instructions. 

And thats a definition ruleset, a programming language, like C++.

 

C++ have support for very basic instructions like adding numbers together and through use of standard 

librarys reading and writing of files for example. Theres no defined rules for drawing graphics on the 

screen, but you still dont need to use machine code (binary instructions or assembly) to do so. 

A library called SDL will handle many difficult things with ease.

 

When talking about programming you talk about things like "librarys" and "variables", thats because 

it eases communication when people describe the same things with the same choice of words, its also 

good representations of what it actually is.

 

A library for example is a collection of source code thats already been written by someone or someones. 

By including a library to your own source code you basically copy and paste all the code in the file 

into your own.

 

The CPU only deals with numbers, so how do you tell the CPU you want pixel 45, 78 of the display to be 

filled with green color because it should be part of a grassy landscape? You define a ruleset! 

For example, if you stand on a position in the landscape and look in a direction you will see this 

or that. The position of everything is described with numbers, like value 77 for X dimension. 

That means you can use math to calculate the world into the image you should see on the display, 

and you send the color value to the right adress in the GPU memory and you will get what you want 

on the screen.

 

 

Lets start learning the C++ language!

 

Storing data in the memory is done by the use of "variables". A variable is an adress to somewere in 

the RAM that you can choose to refer to with a name of your own choice. 

Since RAM adresses are a number sequence its difficult to keep track of what data is were manually. 

By typing "int hitpoints;" you reserve 4 bytes of RAM memory, the compilator that translates your 

source code to machine code will automatically deal with finding available memory and reserving it, 

and whenever you refere to "hitpoints" you will end up in the desired position in the RAM memory.

 

A variable is created by "declaring" it, its simply done by selecting a data format you wish to use 

and supplying a name of your choise and finally ending it with a ";" symbol.

The C++ tutorial refers to this semicolon ( ; ) as marking the end of a statement. 

http://www.cplusplus.com/doc/tutorial/

 

I cant think of a good way of explaining them, just think of it as the end of an action. 

As you read source code you will see how its used and once you have come to understand the basics 

of how a program works you should be able to understand more technical explenations. 

 

A data format for variables describes how much RAM space that is reserved. An "int" takes 4 bytes, 

a "double" takes 8 bytes. Since its so little space and modern hardware have so much RAM memory 

you dont have to worry about using a "char" instead of an "int" if you can get away with it, but 

if you create large amount of data using the least required storage becomes important, you will 

understand this later. 

 

So why use different data formats? Storing numbers takes up space, a char can only store numbers 

between -128 to 127, or between 0 to 255 if you choose a different scale. 

The range of numbers and space required to store them can be seen here: 

http://www.cplusplus.com/doc/tutorial/variables/

 

We will stick to int and float in the beginning, worry not about the size here, instead worry 

only about what can be stored in the different data formats. 

An int can only store whole numbers, 1, 2, 3, 4 and so on. If you wish to store decimal numbers 

you have to use a float, 1.1, 1.2, 3.456, 4.01234567 and so on. 

Theres a limit to how many decimals you can use, some people say 6 is a safe bet on all hardware, 

in my experience 8 is the best to use, more then that and numbers can fall off or what to say, 

they are simply not stored, and anything that isnt stored cant be read. 

When dealing with decimal number math, because the computer cant process an endless amount of 

decimals you will always get inacurate result, this has to be taken into account when coding 

anything that requires high precision. 

 

For example, if you create a float variable and store the value 10.0 in it, then devide it by 3, 

then mutiply it with 3, you will end up with the value 9.99999999 and not 10.0. Remember that! 

 

 

Now lets look at some source code finally! 

************************************************************************************************

 

#include "iostream"

using namespace std;

int main (){

cout<<"Hello World!"<<endl;

return 0;}

 

***********************************************************************************************

Here is a complete program, you can compile it on the Pandora by starting CDEVTOOLS, enter your 

system password, then type "g++ -o helloworld hw.cpp". If all went well no error messages 

popped up and you can now run the program by typing "./helloworld". 

 

It contains of the following parts, an inclusion of a library, a definition, and a function that 

performs a single operation. 

The command "#include" adds all text from the file refered to, in this case iostream wich is 

a standard C++ library. We add this library because it contains an object called "cout" that 

we use to print out text in the terminal window. 

The command "using namespace" tells the compiler were in the source code to look for things, 

I have always thaught about this as chapters in a book, the "iostream" code is a book and "std" 

is a specific chapter in that book that contains the definition for "cout". 

 

Then comes something that looks like a variable, "int main(){". This is something else, 

its what is called a function, I think of it as a small program inside the large one. 

You can have as many functions as you want, and they can be inside one and another, 

but every program needs a specific function called "main", that defines were in the program 

the CPU starts processing instructions from.

 

A function is declared by choosing a data format and a name of your choice (except in the case 

of "main"), and a parameter section defined with parantesis symbols (), and lastly what is 

called a body, defined by curly braces {}.

 

Why does a function need to be of a data format? Because they return a value, unless theyre 

of the type "void". A function that is anything but void type needs the command "return", 

in the case of main function you return a value to the operative system, it signals if the 

program finnished without a problem (if the value returned was "0") or if it crashed.

 

If you create a variable (declare it), by typing "int lol;", you can the use that variable 

to store integer numbers (whole numbers), typing "lol=5;" would store the number or value 

5 in it. Typing "lol=lal();" would store whatever value the function "lal" returns. 

So if a function is like a small program in itself, typing "lol=lal();" would activate 

that function by request and the program will now go through it command after command 

until it finnishes.

 

Lets make a test: 

*******************************************************************************************

 

#include "iostream"

using namespace std;

 

int lal(){return 7;}

 

int main (){

  

  int lol;

  lol=lal();

  cout<<lol<<endl;

  

return 0;}

 

*********************************************************************************************

This will print out the number 7 on screen.

As you see we dont use quotation symbols in the cout command now ("), the quotation symbols 

are used to signal the use of text, but we dont want to print out the text "lol" on screen, 

we want to print out the value inside the lol variable. "endl" is a defined command to 

start a new line in the terminal window. 

 

Since all variables are numbers, or values representing something according to your definition, 

variables can be used for math operations, that means you can add two variables together. 

 

Lets experiment with some more advanced code.

**********************************************************************************************

 

#include "iostream"

using namespace std;

 

int lal(){

  cout<<"Whats up?"<<endl;

  int a=8,b;

  cout<<b<<endl;

  b=1;

  return a+b;}

 

int main (){

  

  int lol;

  lol=lal();

  cout<<lol<<lol-1<<" "<<lol-2<<endl;

  

return 0;

}

 

**************************************************************************************************

On my computer this program prints out:

 

Whats up?

134513901

98 7

 

The middle line will look different for you. So what happens here? 

In the function "lal" we create 2 variables called "a" and "b", since they are both the same data 

format we can declare multiple variables at once by seperting them with a comma (,). 

We also assign the value 8 to "a" at the same time that we declare it. 

We dont however assign a value to "b", and when we print out the value of it on the following line 

we get any random value, the compiler reserved an adress marked by the operative system as available 

for use but it never changed tha value that was previously stored there. 

Remember this, if you dont assign a specific value to a variable before you use it the value can be 

anything, it is therefor important to not forget this when using variables for math operations. 

 

Now for some more code:

****************************************************************************************************

 

#include "iostream"

using namespace std;

 

int main (){

  

  int lol;

  lol=5;

  lol+=1;

  lol=lol+1;

  cout<<lol<<endl;

  

  int lal=8;

  lal+=lol;

  lol=2;

  cout<<"lol = "<<lol<<", lal = "<<lal<<endl;

  

  float hmm;

  hmm=lal/lol;

  cout<<"hmm = "<<hmm<<endl;

  

return 0;

}

 

*******************************************************************************************************

Now we come to something that has the potential of causing an error!

The line "hmm=lal/lol;" is essentialy "15/2", wich should be 7.5. But when we print out the value of 

hmm we get a 7! Why is that? Its because both variables used in the calculation are integer data 

format, because of this fact the calulation will be processed using only whole numbers, we can change this 

by typing "hmm=(float)lal/lol;", by inserting a "(float)" we tell the compiler the calculation should 

be done using floating point math, and the resulting value in hmm will be 7.5. 

Remember that! When mixing variables of different data formats you need to tell the compiler if you 

wish for the calculation to be processed in a specific format, like (int), (float), or even other 

for various other processes that you will see in the future. 

 

Now lets look at a loop! 

A loop is something that repeats until a definition becomes false, you therefore need to be careful 

when creating loops, if you miss to make a possibilty to get out of them they can repeat FOREVER! 

A loop looks just like a function, but you dont need to declare it with a data format and it dosnt 

return anything. The parameter section is used to define the conditions for repeating, lets look at 

some actual code:

 

**************************************************************************************************

 

#include "iostream"

using namespace std;

 

int main (){

  

  int lol=0,a=0,lal=0;

  

  while(a<5){

    lol++;

    a+=1;

  }

  cout<<lol<<"   "<<a<<endl;

  

  for(int b=0;b<=10;b+=5){

    lol+=b+a;

    lal+=1;

  }

  cout<<lol<<"   "<<lal<<endl;

  

return 0;

}

 

****************************************************************************************************

Here we see 2 different loops, a "while" loop and a "for" loop, they both achieve the same thing and 

should be equal in performance so wich loop you choose to use for whatever situation is a matter of 

taste. Lets look at the "while" loop, it will repeat all commands that are in its body (inside the 

curly braces) until the definition in the parameter section becomes FALSE. 

The definition in this case is "a<5". It means that as long as the value in the variable "a" is less 

then 5, the loop will repeat. What happens inside the body then? 

The "lol" variable uses the incremental operator, that adds the value 1 to it. And the "a" variable 

gets the value 1 added by the compound add operator. If we didnt increase the value of "a" inside 

the body the condition for repeating would never become false and the loop would go on forever. 

 

The for loop looks different, its parameter section requires 3 seperate commands, first create 

a variable used for the condition of repeating, then define the condition, here its for as long 

as "b" is less then or equal to 10, then lastly a command that happens for every cycle of the loop. 

 

Now lets talk about scopes, a variable have a scope of visibilty. It means a variable only excists 

inside the function it was created in, a loop probably dosnt count as a function but in this regard 

it acts as one, the "b" variable is only visible inside the for loop! If you tried to print out the 

value of "b" otside the for loop you would get an error message and the compiler would fail to make 

a program. The scope have inheritance, a variable declared outside the main function is visible by 

all functions below it.

Lets look at that in some code:

 

**********************************************************************************************

 

 

#include "iostream"

using namespace std;

 

int what=3;

 

int lol(int& B) {

  b+=1;

  return 0;}

  

int lal(){

  if(what==3){cout<<"hmm?"<<endl;}

  return 0;}

 

int main (){

  int a=0;

  int funny;

  

  lol(a);

  funny=lal();

  cout<<a<<endl;

  

return 0;

}

 

 

***************************************************************************************************

 

Heres a couple of new things, the "if" statement, and a function that manipulates the value inside 

a variable that is not inside its scope! 

Lets begin with the new function use, in the parameter section of "lol" you will find a "&" symbol 

after int, the "&" symbol is used to signal the use of the adress to the memory, rather then the 

value stored in that adress. When we activate the function we pass data to it through the parameter 

section, we pass the value thats in "a", to pass data to a function you must create as many variables 

in the parameter section of the function creation as you wish to pass, and they must also be of 

the same data types, you can only pass an int type to an int, and a float to another float and so on. 

 

To pass one integer value to a function all you need is a int variable in the parameter section, 

but if that int declaration is followed by the adress operator it will pass data out aswell as in. 

That means that when you activate "lol" and pass "a" as a parameter, once the function has completed 

its commands "a" will recieve whatever value the corresponding function variable used will have at then. 

 

Now lets look at the "if" statement, its very similiar to a function and a loop, it has a parameter 

section and a body. The commands inside the body will only be processed if the definition in the 

parameter section results in a TRUE. In this case the definition is TRUE, double "=" symbols is the 

equality operator, a single "=" is the assign operator, using the assign operator means you change 

the value to something. Using the equality operator you only compare things, here you compare the 

value in "what" to 3, and if they are the same it results in TRUE otherwise FALSE.

hw.cpp.zip
 

Attachments

  • hw.cpp.zip
    240 bytes · Views: 139
@mjohannson nice intro there - a couple of notes


- variables don't have to be numbers. They can contain strings or other data types.


- though you are using C++ includes, you are basically illustrating C programming - loops, variables, functions. I think this is a good way to introduce programming, but you could make your example program a C program instead.


- to learn how to pass C values by reference this is a good guide http://clc-wiki.net/wiki/C_language:Terms:pass_by_reference
 
klapse, yea good points, I was gonna introduce char arrays later, I think in part 3, I just finnished part 2 now. 
 
Ok heres part 2! And Ill guess Ill demonstrate some of that magic of arrays in part 3... :p

 

 

I dont know how I could forget to mention "arrays" since its my favorite thing ever! 

An array is a collection of variables, if you type "int a,b;" you create 2 variables, 

they can have any position in the memory and can be far apart from eachother in terms of adresses. 

The beauty of arrays is that all the memory reserved will be right next to one and another 

in terms of adresses, why would that matter? I will demonstrate the miraculous nearly magical 

possibilitys later. 

 

To create an array declare a data format and a name of your choise, just like a normal variable, 

but follow it with the square parentesis symbols []. Like this: "int lol[2];". 

That will create an array with 2 "elements", an element is like a single variable, so this will 

create 2 integer variables that lies besides one and another in the RAM memory. 

To access them you type the number of the element you wish to use, and then count of the elements 

start from 0, when you create 2 elements you type 2 for the number you wish to create, 

but once its created then when you use it you need to start counting from 0, so to access element 

2 you type "lol[1]=5;", that will store the value 5 in the second element. 

 

Now lets get some graphics on the screen finally! 

**************************************************************************************************

 

#include "iostream"

#include "stdlib.h"

#include "SDL/SDL.h"

 

using namespace std;

int main(){

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);

SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE|SDL_NOFRAME);

SDL_Event game;

SDL_Rect temp;

Uint8* key;

int quit=0;

Uint8 r=255,g=255,b=255;

int currentTime,oldTime,framerate = 10;

 

 

  int xp=80,yp=80,tv1,tv2,tv3;

  int tv4;

  int change_color=0,change_color_pressed=0;

  

while (quit==0){

 

  while (SDL_PollEvent(&game)){}

 

   key=SDL_GetKeyState(NULL);

   if(key[sDLK_ESCAPE]){quit=1;}

   if(key[sDLK_a]){change_color=1;}else{change_color_pressed=0;}

   if(key[sDLK_UP]){yp-=1;}

   if(key[sDLK_DOWN]){yp+=1;}

   if(key[sDLK_LEFT]){xp-=1;}

   if(key[sDLK_RIGHT]){xp+=1;}

   

   if(xp<0){xp=0;}

   if(xp>200-40){xp=200-40;}

   if(yp<0){yp=0;}

   if(yp>200-40){yp=200-40;}

 

   tv1=currentTime;

   tv2=oldTime;

   tv3=(tv1*tv2)/90909;

   

   if(change_color==1&&change_color_pressed==0){

    srand(tv1);

    r=rand()%255;

    srand(tv2);

    g=rand()%255;

    srand(tv3);

    b=rand()%255;

    cout<<"R = "<<(int)r<<", G = "<<(int)g<<", B = "<<(int)b<<endl;

   change_color_pressed=1;}change_color=0;

   

temp.x=0;

temp.y=0;

temp.w=200;

temp.h=200;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,0,0));// CLEAN THE SCREEN.

 

temp.x=xp;

temp.y=yp;

temp.w=40;

temp.h=40;

SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,r,g, B) );

 

 

SDL_Flip(dis);

 

currentTime=SDL_GetTicks();

if(currentTime-oldTime<framerate)

{SDL_Delay(framerate-(currentTime-oldTime));}

oldTime=SDL_GetTicks();}

 

SDL_Quit();

return 0;

}

 

**********************************************************************************************

Here we have a lot of new stuff! Lets start from the top: 

 

First we include 2 new librarys, stdlib is another standard C++ library. Standard C++ librarys 

are always available with C++ compilers, so you dont need to give a path. If we look below we 

find a library called SDL, short for Simple Direct Media Layer, its an open source library 

thats free for use, you can find more info and tutorials and references on their webpage 

http://www.libsdl.org/

Or google for questions to problems or anything that you might have, theres an abundance of 

information available everywere of this very common library. 

 

We include stdlib because of the functions for generating random numbers, srand() and rand() 

both can be found in there.  

 

The line "SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);" is were we activate different parts of SDL, 

there are many different parts and you might not want to activate them all for the specific 

project you do. We will use SDL_GetTicks() wich is part of the TIMER function and the drawing 

functions are ofcourse part of the VIDEO function. 

 

Next we create a window, "SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE|SDL_NOFRAME);". 

SDL_Surface is a data format, its more complex then a int for example, but dont worry about that now. 

Theres also a "*", this is a symbol used for making pointers, Im not an expert on pointers but I will 

demonstrate them later. 

So what we do is create a variable, but its not a normal variable its a pointer, but you dont need to 

worry about this cos you dont need to know the details in order to be able to make use of it. 

And then we assign something to it at the same time as we create it, we assign a function, and by 

assigning a function you automatically activate it, this function will create the window accordingly 

to the specifications given in the parameter section, 200 pixels wide, 200 pixels high, 24bit color, 

SWSURFACE means it will be processed by CPU alone, HWSURFACE will use the GPU, but I dont know 

to what extent... It might say somewere on the webpage or there might be an explenation to find 

by googling, obviously you dont need to know the specific details of how everything works in order 

to be able to make use of it. NOFRAME means that the window wont have a boarder, wich I personally 

find cool and thats the only reason its there... Its actually better to have a boarder that you 

can grab with mouse to move the window around... Anyway, these last 2 parameters are whats called 

"flags", theyre seperated by a | instead of commas, I suspect it works in a single parameter slot 

and that the function then reads the letters, but mayby Im completly wrong, all you need to know 

to be able to make use of it is that you can add as many flags as you wish and wichever you want, 

just seperate them with | symbols instead of commas. 

 

Next comes another SDL specific data format, SDL_Event, we create a variable of this format that 

will be used to read input from the keyboard. SDL_Rect is a good demonstration of more complex 

data format as you will se later in the code. We will use the SDL_Rect variable to store 

position and width/height of a drawing function. 

 

Next up we have something I cant explain, "Uint8", U stands for unsigned wich defines the scale 

of the number range that can be stored in the variable, as you can see here:

http://www.cplusplus.com/doc/tutorial/variables/

The 8 I would assume means that a special integer reserving 8 bytes of memory instead of normal 4. 

Not all data formats are listed on C++ webpage. This "key" pointer variable will be used to store 

the code that keyboard sends. 

 

The other variables created should need no further explenation here. 

Lets instead move on to some action! 

Lets start with "while (quit==0){", this is the game loop so to say, if you exit this loop the 

program will end. 

Next we read the signal that comes from keyboard, "while (SDL_PollEvent(&game)){}" will read 

the event queue, when you press keys on the keyboard they will end up in the queue, and here 

we read that queue. 

 

I think I might be able to explain what a pointer is now, you can access a pointer in two ways, 

access directly or "by reference", you use pointers to store adressess, and if you access the 

pointer by reference you can read or write the memory not in the pointer variable but in the 

variable whos address is stored in the pointer variable. 

 

So if we look at the line "key=SDL_GetKeyState(NULL);", remember that "key" is a pointer, 

what we do here is activate a function, this function will update an array with the status 

of the keyboard, if you press the "K" key the array element representing that key will get 

assigned the value 1, and all keys that arent pressed will recieve the value 0.

 

Below we look at the array elements of the keys we wich to use in the program, I dont know 

how the key pointer works here, or how the elements are found by letters, but again it is 

obviously not necesary to understand exactly how this works in order to make use of it. 

The "if" function will process the commands in its body if the definition in its parameter 

section is TRUE, and "1" is used to symbolise TRUE aswell, so all array elements with value 1 

will be processed. 

 

Here we can also see something new thats a part of the "if" function, "else". 

If the definition of the "if" parameter section is FALSE, the commands in the body of the "else" 

will be processed, if the "if" parameter section is TRUE, the commands in the body of the "else" 

will simply be ignored. The "else" must follow directly after the body of the "if" function 

it belongs to. 

 

If a key remains pressed the value of the array element will be 1 every time we read it, that means 

if we use it to do something like move left on the screen, we will continue to move left until we 

release the key. This behaviour isnt always wanted, one way of dealing with it is to use an "if" 

function that checks 2 variables, were one will yield a FALSE value after the "if" function has 

been processed once, this will keep the "if" function from being processed every frame, and then 

reset both variables as key is released allowing for the process to be repeated again. 

 

Lets look at the randomizer functions, "srand(tv1);" is used to establish a reference or something, 

it needs to be set before using rand(). We can enter any value in srand() parameter section. 

Then "r=rand()%255;" is used to give the "r" variable a random value between 0 and 255. 

If we were to use rand() immidiatly again for the "g" variable, it would end up generating the 

exact same value, so we need to once again set the reference in srand() to a different value. 

 

Now lets look at "temp.x=0;", "temp" is a variable with a complex data format, this particular 

format can hold several values, it has what is called "member variables" or something like that. 

These individual members are accessible by entering their respective names after the main variables 

name seperated by a dot. In the line "SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,0,0));" we use 

the "temp" variable in the parameter section of the function, the function will use the adress of 

the variable to automatically get all the data it wants from the member variables, so it has 

similiarity to both arrays and pointers. The "&" symbol is used to signal the use of adress of a 

variable rather then the value stored inside. 

 

The SDL_FillRect() function will draw a square on the screen, the first parameter is a surface, 

you can use as many surfaces as you like and draw individually to all of them and draw from one 

surface to another, but here we use one single surface to cover the screen. First we pass the 

surface as a parameter, then the "temp" variable that we have previously update with coordinates 

describing how big rectangle we wish to draw and were it should be placed on the surface. 

The temp.x defines the starting place, temp.w defines how long it stretches to the right of that 

position. The coordinates 0,0 is lower left corner of the screen, since we have a window of size 

200,200 that will also be the coordinates for the top right corner of the screen. 

 

Now we may use different parts of the surface then 0-200, think of the surface as an endless 

canvas, of wich we then overlay the screen window wich is 200 pixels wide and high. That means 

that if we draw the canvas at -50 in X dimension the game world so to say will be displayed 

50 pixels to the left, if you would run fowards to the right the screen would scroll along, 

it means that an object with position -40 in X dimension would be visible on screen 10 pixels 

to the right of the left edge initially, but as screen moved fowards the object would 

disapear to the left off the screen, even though the coordinates of the object remained the 

same, because we move the coordinates of the camera so to say, and we start drawing from 

different position, different coordinates of the game world. 

 

After the drawing function you see this "// CLEAN THE SCREEN.", a double slash "//" is 

a commentator operator, the compiler will ignore all text that follows for the rest of that line. 

You should use this function to comment on the source code you write, becasue code can become 

very complex and very long, its therefor easy to forget what everything does and how things 

may work, so remember to make good commenting to describe things as well as possible, 

doing so will save you a lot of time having to study code and figure out once again what it is 

and how it works. 

 

Clean the screen, what I do there is fill the screen with black color, if I hadnt done that 

the graphics memory would remain the old values, the values only change when you update them. 

So if a caracter moves along the screen it will leave a trail behind it, once you have graphics 

filling the entire screen and updating every frame it will naturally be prevented, but here and 

now we dont want to fill the screen with "trails". 

 

Next we draw our "caracter", we use the position of "xp" and "yp" variables, those values are 

affected by the keyboard. 

 

Now we have drawn graphics on the surface, now its time to send that data to the screen, 

we do this with "SDL_Flip(dis);", it will update the GPU buffert that is used to fill the pixels 

on the screen with color according to the values stored in that buffert. We pass the surface 

we want to send as a parameter. 

 

Next we have my way of limiting the framerate, without a limiter the program would process as 

many frames as the CPU would be able to per second. If a caracter move 1 pixel per frame, and 

if the CPU could process 1000 frames per second that means the caracter would move 1000 pixels 

per second. By measuring the time we can use a delay function to limit the framerate, likewise 

its good practise to make "framerate independant" physics, that means instead of moving a 

caracter 1 pixel per frame, you move it how many pixels you want it to move per second but 

multiply it with the time value you make. How do you create such a time value? 

 

With SDL_GetTicks() you will recieve a value that starts counting at the time you initialise 

the SDL_TIMER function. This value is counted in 1000 per second. So if you measure the time 

between frames, you can calculate the framerate and create a value to multiply the physics with. 

 

For example, if we use 2 variables to get the time between frames, we can subtract the old 

time value from the new, then we might get something like the value 50. If we then divide this 

by 1000 we end up with what is how many percentage of 100% we wish to multiply the physics with. 

If framerate is to slow the value might become more then 100%. Since 1.0 would represent 100%, 

0.5 represents 50%. I have a feeling Im not explaining this very well, but you will find a way 

to achieve it thats comfortable to you. 

 

Next we have SDL_Delay(), very simple, pass the time you want as the parameter, remember 1000 

is 1 second. Then the program will essentialy freeze until that time have passed. 

Freeze ofcourse means idle as in not using the full CPU capacity it would otherwise if we had not 

limited the framerate. 

 

And lastly we have the function SDL_Quit(), this cleans away all the memory SDL reserves when 

you activate it. 

 

 

 

And this is compiled by typing "g++ -o game1 game1.cpp -lSDL", the -l stands for link and is 

needed for some librarys. I think this has to do with dll files, dynamically linked library, 

but I may be wrong... However if you were to compile this program for Windows for example, 

you would need a sdl.dll file in the same folder as the .exe file to be able to run it, 

if you install SDL on Linux I dont think you need to worry about this, the OS handles it 

automatically I think. 

 

And to run the file type "./game1" or click on it in the file browser! 

 

game1.cpp.zip
 

Attachments

  • game1.cpp.zip
    866 bytes · Views: 146
Perhaps these belong in the new content management system ED is setting up?
 
- "Next up we have something I cant explain" - remove this and explain uint8 - uint8 takes 8 bits, not bytes of memory


- You got the srand() and rand() functions wrong. srand() SEEDS the random number generator. For a given seed, subsequent calls to rand() rill give the same sequence of pseudorandom numbers, so you want to seed srand with a different number ONLY ONCE every time you run the program. http://www.cplusplus.com/reference/cstdlib/srand/ Dont call srand with tv1 as a parameter - use something like the system time: srand(time(0)); and that will give you a different seed every time the program is run.


A good first effort, but I'm sure you know a few things could be reworded for better clarity. Let a couple of days pass, then reread the document carefully to see if some things could be stated more clearly. Don't apologize for your explanations - improve them (use google to find other explanations of the same thing).
 
so you want to seed srand with a different number ONLY ONCE every time you run the program. http://www.cplusplus.com/reference/cstdlib/srand/ Dont call srand with tv1 as a parameter - use something like the system time: srand(time(0)); and that will give you a different seed every time the program is run.
I disagree with this. There are times you want, and times you need, to reseed. For example, in my Freecell game, you reseed at the start of every game with the game number, same with CyRacer.
 
Back
Top