59
GAME PROGRAMMING IN C Workshop India Wilson Wingston Sharon [email protected]

C game programming - SDL

Embed Size (px)

DESCRIPTION

An introduction to Game programming in C using the SDL graphics engine.

Citation preview

Page 1: C game programming - SDL

GAME PROGRAMMING IN CWorkshop IndiaWilson Wingston [email protected]

Page 2: C game programming - SDL

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

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

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
Page 6: C game programming - SDL

Files thatBelong to the Current project

CodeFor theFile that is currentlyOpen in the tab

Page 7: C game programming - SDL

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

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
Page 10: C game programming - SDL

When you run, you’ll get a white pixel on a black screen

Page 11: C game programming - SDL

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

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

Lets talk about games

Page 14: C game programming - SDL

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

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
Page 17: C game programming - SDL

init() - in graphics.h

Page 18: C game programming - SDL

init()

Page 19: C game programming - SDL

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

Coming back to main()

Page 21: C game programming - SDL

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

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
Page 24: C game programming - SDL

NEAT code helps

Page 25: C game programming - SDL

Render() function

Page 26: C game programming - SDL

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

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

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
Page 30: C game programming - SDL

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

This should be your output.

Page 32: C game programming - SDL

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

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

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

Updating the point variable

Page 36: C game programming - SDL

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
Page 38: C game programming - SDL

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

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

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

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

Compile with this code:

Page 43: C game programming - SDL
Page 44: C game programming - SDL

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

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
Page 47: C game programming - SDL
Page 48: C game programming - SDL

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

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

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

In render()

Page 52: C game programming - SDL
Page 53: C game programming - SDL

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

Collision detection of two circles.• First we create a struct circle. (same like point

except with radius too).

Page 55: C game programming - SDL

Initialising the circles

Page 56: C game programming - SDL

In render()

Page 57: C game programming - SDL

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

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

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.