View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
• 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.
• 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.
• 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.
Code For the File that is currently Open in the tabFiles thatBelong to theCurrentproject
-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.
• 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.
• 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.
• 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.
The game itself Here the logic that specifies how the game behaves to the user Game EngineSDL just gives as access to graphics card. SGE is our game engine. SDL Simple Direct Media Layer Provides a layer to access graphics card Graphics Card on computer Harware support for drawing stuff onto the screen
• 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
• This must be a global object.• Whatever we want SDL to display has to be written to this screen.•
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_FULLSCR EEN• To the last argument of the function, the program goes into full screen mode!
Update Draw graphics game models elementsUndraw Get input what is from not userrequired Check Game logic
• 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!
• 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? •?
• 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()
• 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.
• 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.
• 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?
• 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. Position Keyboard is Screen is Point is press is updated cleared drawn checked for the point
• 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.
• 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.
• Code comes at the level of the last one.• Case SDL_KEYUP:• What to dowhen the key has beenreleased?Set the move variable m1 kUD or KLRto 0. Signifying no movement.
• cls(0) fills the screen with black;• Updates point p1 with m1 for which direction to move the point• Putpixel to draw the point.
• 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.
• 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
• 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.
• 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
• 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!
• 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.
• 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,
• 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
• First we create a struct circle. (same like point except with radius too).
• Same KEYUP/KEYDOW N movement code to move m1 and m2.• Adjust this code and get the second circle moving too.
• 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.
• 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.