c game programming - sdl
DESCRIPTION
An introduction to Game programming in C using the SDL graphics engine.TRANSCRIPT
![Page 1: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/1.jpg)
GAME PROGRAMMING IN CWorkshop IndiaWilson Wingston [email protected]
![Page 2: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/2.jpg)
Lets start with games• Why?
• Learning is not just about marks.
• Its about building something.
• You cannot learn C in 2 weeks. • You can only learn C by writing programs.• Without writing programs OF YOUR OWN and ON YOUR
OWN.• We can only guide you – you have to put in some
effort.
![Page 3: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/3.jpg)
Lets start with a setting up your environment• In todays session file, you will see some files like
this.
• Install all of them.
• Now see the Dev-Cpp folder?
• Copy everything in that to your Dev-Cpp folder (C:/Dev-Cpp). Copy the folders *inside* this one to the one in you C drive.
![Page 4: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/4.jpg)
How to check if everything alright?• In the /game folder, you will see a folder called
“SDL_Template(copy this folder)”
• Copy this folder to your own folder in your Exercise drive.
• After copying, rename the Folder to something like “SDL_1”
• In the folder you will see sdl.dev, you will have to open this file.
• You can open dev-cpp normally (if you need to enter passwrd) and then use the file>>open project or file>> to open this sdl.dev project file in your folder.
![Page 5: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/5.jpg)
![Page 6: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/6.jpg)
Files thatBelong to the Current project
CodeFor theFile that is currentlyOpen in the tab
![Page 7: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/7.jpg)
Press alt-P for project options-lmingw32-lSDLmain-lSDL-lSGE
• The above parameters• Must be present in• The linker tab
• Say OK• Press Ctr-F11to build• And then F9 to run the program.
![Page 8: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/8.jpg)
Errors you might get• If it doesn’t work.
• Recheck your project options tab• See if you installed everything in the folder?
• Does it complain about missing dll? See if all files in the project folder conform to the folder image provided 3 slides before.
![Page 9: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/9.jpg)
![Page 10: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/10.jpg)
When you run, you’ll get a white pixel on a black screen
![Page 11: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/11.jpg)
Once you’ve finished, don’t call us• Help your friends set it up
• Learning is a collaborative process. Se what mistakes your friends have made. Check them. Help them.
• It makes you a better programmer by exposing you to the mistakes that are possible in C.
• It’ll help you avoid silly mistakes if you help others by correcting them.
![Page 12: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/12.jpg)
There are 2 filed in the project• main.cpp• this file contains the main() function• This is called the entry point of the program.
• grapics.h• This file contains some structures and function that I
wrote.• You might or might not use them as per requirement of
the program.
• The thing is well commented and documented.
![Page 13: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/13.jpg)
Lets talk about games
![Page 14: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/14.jpg)
Header files necessary
• make sure you have copied the Dev-Cpp folder i gave you onto your Dev-Cpp installation.
• That links up all sort of libraries for SDL to function• Otherwise you might get an error about missing SDL.h
![Page 15: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/15.jpg)
SDL_Surface *screen
• This must be a global object.• Whatever we want SDL to display has to be
written to this screen.
•
![Page 16: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/16.jpg)
![Page 17: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/17.jpg)
init() - in graphics.h
![Page 18: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/18.jpg)
init()
![Page 19: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/19.jpg)
The init() functon
screen = SDL_SetVideoMode(MX, MY, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);The above line is important• If you change MX and MY (in the beginning of
graphics.h) you change the size of screen
• If you add, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN
• To the last argument of the function, the program goes into full screen mode!
![Page 20: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/20.jpg)
Coming back to main()
![Page 21: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/21.jpg)
Gaming cycle
Draw game elements
Get input from user
Check Game logic
Undraw what is not required
Update graphics models
![Page 22: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/22.jpg)
Events• Events are things that happen outside the game
engine that the game must be aware of.
• Eg: Keypresses
• SDL_pollevent(&event)• Returns NULL when there are no events left to handle, so
then that while loops terminates and the game continues.• The event object contains details about what happened
and the rest of the code handles it!
![Page 23: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/23.jpg)
![Page 24: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/24.jpg)
NEAT code helps
![Page 25: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/25.jpg)
Render() function
![Page 26: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/26.jpg)
Remember.• Don’t touch the init() function.
• To add user interaction, code has to go in which function?• ?
• To add some more drawings, code has to go where?• ?
![Page 27: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/27.jpg)
Remember.• Don’t touch the init() function.
• To add user interaction, code has to go in which function?• The event managing while loop in the main() function.
• To add some more drawings, code has to go where?• The render() function.• Or another function between init() and the eventhandler()
![Page 28: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/28.jpg)
graphics.h• Here are codes for most functions and all
• The main.cpp and graphics.h need to *share* the same global variable screen.
• This is done by the keyword extern; this specifies that this is not a different variable, but a same one that is declared in another file.
![Page 29: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/29.jpg)
![Page 30: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/30.jpg)
Exercise 1: draw a horizontal line• The function _putpixel
draws a point at the MX/2 and MY/2 position.
• Modify the code to draw a horizontal line.
• ________________________
• Across the screen.
![Page 31: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/31.jpg)
This should be your output.
![Page 32: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/32.jpg)
Put a for loop• That for loop starts from i
=0• Till MX in steps of 1
• And the putpixel function draws a pixel at every point.
• Can you change it to vertical?
![Page 33: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/33.jpg)
Moving Events!• What to do when considering to move a point.• What all is required to define a point?• X• Y• Colour.
• Keyboard handling is done on the event loop.
Keyboard
press is checked
Position is updated for the point
Screen is cleared
Point is drawn
![Page 34: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/34.jpg)
Checking keyboard presses• Keyboard presses is update as
a struct in graphics.h called move.
• Each point will have an associated move variable.
• If kLR is -1/1 the point will have to move Left/Right.
• And so on.
![Page 35: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/35.jpg)
Updating the point variable
![Page 36: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/36.jpg)
Keyboard handle loop• It is the while(SDL_Pollevent(&event))
• For every loop, the event structure will have the details of what happens, we want to see onky keyboard events.
• These are events of event.type == • SDL_KEYDOWN • SDL_KEYUP• Etc..
• Using a switch case we can write what code to happen when each event is fired.
![Page 37: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/37.jpg)
![Page 38: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/38.jpg)
KEYUP• Code comes at the level of the last
one.• Case SDL_KEYUP:
• What to dowhen the key has been released?
Set the move variable m1 kUD or KLR to 0. Signifying no movement.
![Page 39: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/39.jpg)
In Render()
• cls(0) fills the screen with black;
• Updates point p1 with m1 for which direction to move the point
• Putpixel to draw the point.
![Page 40: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/40.jpg)
So…• Render() runs infinitely,
• Checks if point p1 has moved by using the m1variable• If moved, update the point’s x & y• Then clear the screen and draws the point.
• When keyboard is pressed• The eventloop() in main now enters the loop• A switch case is done on the event type to determine if it’s a keypress
or a release.• When the keypress is detected the m1 variable is adjusted• When the keyrelease is detected the m1 variable is set to 0 so not
movement happens.
• Compile 2.Movment folder code.• Again: follow dev-cpp rules for opening projects and compiling them.
![Page 41: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/41.jpg)
Modifying Render! Drawing More• We have the point p1 that we can
move around with the keyboard.
• Lets draw a line between p1 and MX/2, MY/2.
• That is a line from p1 to the center.
• Line function:• sge_Line(*screen,x1,y1,x2,y2,color);• sge_AALine(…) is line with anti-
aliasing
![Page 42: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/42.jpg)
Compile with this code:
![Page 43: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/43.jpg)
![Page 44: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/44.jpg)
YOUR EXERCISE:• I want the other points to move by using the • W - UP• S - DOWN• A - LEFT• D – Right
• Point 2 is handled by m2.• Using switch case do the update of m2• And update p2 with m2
• I want the line to move by both points. • One point moves by arrow keys• The other moves by wasd keys.
![Page 45: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/45.jpg)
Lets talk about shapes.• The SGE SDL library provides some basic
primitives for drawing.
• Drawing in this case is mathematical drawing.
• The basic shapes that are provided are:• Points• Lines• Circle• Rectangle• Ellipse• Polygons
![Page 46: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/46.jpg)
![Page 47: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/47.jpg)
![Page 48: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/48.jpg)
Play around with these things.• Try to think of creative shapes and things that you
can draw with these functions.
• You can also make interesting mathematical loop shapes.
• If you don’t do the cls(0) whatever the loop draws wont be erased in the next iteration!.
• Try and do some more hoola!
![Page 49: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/49.jpg)
Texting with SDL• SDL printing things on the screen
means you need a font!
• The font file is *something*.ttf
• These sort of fonts can be opened by SDL and drawn onto the screen.
• First we need to make a global font * variable.
![Page 50: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/50.jpg)
Then in main()• We need to initialise the font • We have cour.ttf right now.
• sge_TTF_Init() initialises the font engine.
• sge_TTF_openFont(font, size) returns the font object that should be global pointer of the same,
![Page 51: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/51.jpg)
In render()
![Page 52: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/52.jpg)
![Page 53: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/53.jpg)
Exercise• Get text to appear one by one on the screen.
• Get text to scroll around the screen.
• Can you get keyboard input using the event loop ?• Do only numbers
![Page 54: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/54.jpg)
Collision detection of two circles.• First we create a struct circle. (same like point
except with radius too).
![Page 55: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/55.jpg)
Initialising the circles
![Page 56: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/56.jpg)
In render()
![Page 57: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/57.jpg)
Keyboard loop• Same
KEYUP/KEYDOWN movement code to move m1 and m2.
• Adjust this code and get the second circle moving too.
![Page 58: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/58.jpg)
GAME• Instead of moving the second circle use the below
random functions
• int sge_Random(int min, int max)Returns a random integer between (and including) min and max.
void sge_Randomize(void)Seed the random number generator with a number from the system clock. Should be called once before the first use of sge_Random.
![Page 59: C game programming - SDL](https://reader035.vdocuments.us/reader035/viewer/2022062220/555a0411d8b42ad00a8b5131/html5/thumbnails/59.jpg)
Objectives• Move the second circle randomly.
• When the first circle is touching the second circle, a counter should start in the corner.
• As long as the two circles are touching, counter must keep incrementing and the second circle keeps moves faster and faster.
• When the first circle leaves, the counter goes to 0 and stays there.