SlideShare une entreprise Scribd logo
1  sur  59
Workshop India
Wilson Wingston Sharon
wingston.sharon@gmail.com
• 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 tab



Files that
Belong to the
Current
project
-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 Engine

SDL 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             elements




Undraw
                              Get input
 what is
                               from
  not
                               user
required

                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 been
released?

Set the move variable m1 kUD or KLR
to 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.

Contenu connexe

Tendances

Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
topomax
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
changehee lee
 
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
OGDC 2014_Architecting Games in Unity_Mr. Rustum ScammellOGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
ogdc
 

Tendances (8)

Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
 
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
 
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
OGDC 2014_Architecting Games in Unity_Mr. Rustum ScammellOGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
 
Best Practices for Shader Graph
Best Practices for Shader GraphBest Practices for Shader Graph
Best Practices for Shader Graph
 
Game Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content GenerationGame Programming 07 - Procedural Content Generation
Game Programming 07 - Procedural Content Generation
 
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
 
ECS: Making the Entity Debugger - Unite LA
ECS: Making the Entity Debugger - Unite LAECS: Making the Entity Debugger - Unite LA
ECS: Making the Entity Debugger - Unite LA
 

En vedette

Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 

En vedette (20)

Srs format
Srs formatSrs format
Srs format
 
Game Development with SDL and Perl
Game Development with SDL and PerlGame Development with SDL and Perl
Game Development with SDL and Perl
 
Quiz
QuizQuiz
Quiz
 
C language in our world 2015
C language in our world 2015C language in our world 2015
C language in our world 2015
 
General Quiz by Quiz pro. co. ,VNIT
General Quiz by Quiz pro. co. ,VNIT  General Quiz by Quiz pro. co. ,VNIT
General Quiz by Quiz pro. co. ,VNIT
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
 
TMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and ReportsTMS - Schedule of Presentations and Reports
TMS - Schedule of Presentations and Reports
 
Using Git on the Command Line
Using Git on the Command LineUsing Git on the Command Line
Using Git on the Command Line
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth Impact
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - Exercises
 
FLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - ExercisesFLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part VI - Sixth Impact - Exercises
 
Manipulating file in Python
Manipulating file in PythonManipulating file in Python
Manipulating file in Python
 
Git hooks For PHP Developers
Git hooks For PHP DevelopersGit hooks For PHP Developers
Git hooks For PHP Developers
 
Servicios web con Python
Servicios web con PythonServicios web con Python
Servicios web con Python
 
FLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second ImpactFLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part II - Second Impact
 
FLTK Summer Course - Part VII - Seventh Impact
FLTK Summer Course - Part VII  - Seventh ImpactFLTK Summer Course - Part VII  - Seventh Impact
FLTK Summer Course - Part VII - Seventh Impact
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
 

Similaire à C game programming - SDL

[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Sang Don Kim
 

Similaire à C game programming - SDL (20)

Computer Graphics with OpenGL presentation Slides.pptx
Computer Graphics with OpenGL presentation Slides.pptxComputer Graphics with OpenGL presentation Slides.pptx
Computer Graphics with OpenGL presentation Slides.pptx
 
Soc research
Soc researchSoc research
Soc research
 
Hill ch2ed3
Hill ch2ed3Hill ch2ed3
Hill ch2ed3
 
BSidesDelhi 2018: Headshot - Game Hacking on macOS
BSidesDelhi 2018: Headshot - Game Hacking on macOSBSidesDelhi 2018: Headshot - Game Hacking on macOS
BSidesDelhi 2018: Headshot - Game Hacking on macOS
 
september11.ppt
september11.pptseptember11.ppt
september11.ppt
 
Input and Interaction
Input and InteractionInput and Interaction
Input and Interaction
 
Unit 2 - Complete (1).pptx
Unit 2 - Complete (1).pptxUnit 2 - Complete (1).pptx
Unit 2 - Complete (1).pptx
 
2D graphics
2D graphics2D graphics
2D graphics
 
SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
 
3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx
 
Better Interactive Programs
Better Interactive ProgramsBetter Interactive Programs
Better Interactive Programs
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
Lecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in UnityLecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in Unity
 
18csl67 vtu lab manual
18csl67 vtu lab manual18csl67 vtu lab manual
18csl67 vtu lab manual
 
Micro Keyboarding
Micro KeyboardingMicro Keyboarding
Micro Keyboarding
 
Mini Computer Project
Mini Computer ProjectMini Computer Project
Mini Computer Project
 
Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL
 
Computer Programming In C.pptx
Computer Programming In C.pptxComputer Programming In C.pptx
Computer Programming In C.pptx
 
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
 

Plus de Wingston

03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
Wingston
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
Wingston
 
Introduction to the Arduino
Introduction to the ArduinoIntroduction to the Arduino
Introduction to the Arduino
Wingston
 
4.content mgmt
4.content mgmt4.content mgmt
4.content mgmt
Wingston
 
8 Web Practices for Drupal
8  Web Practices for Drupal8  Web Practices for Drupal
8 Web Practices for Drupal
Wingston
 
7 Theming in Drupal
7 Theming in Drupal7 Theming in Drupal
7 Theming in Drupal
Wingston
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for Drupal
Wingston
 

Plus de Wingston (20)

OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
 
02 hello world - Android
02   hello world - Android02   hello world - Android
02 hello world - Android
 
01 introduction & setup - Android
01   introduction & setup - Android01   introduction & setup - Android
01 introduction & setup - Android
 
OpenCV with android
OpenCV with androidOpenCV with android
OpenCV with android
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Linux – an introduction
Linux – an introductionLinux – an introduction
Linux – an introduction
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linux
 
04 Arduino Peripheral Interfacing
04   Arduino Peripheral Interfacing04   Arduino Peripheral Interfacing
04 Arduino Peripheral Interfacing
 
03 analogue anrduino fundamentals
03   analogue anrduino fundamentals03   analogue anrduino fundamentals
03 analogue anrduino fundamentals
 
02 General Purpose Input - Output on the Arduino
02   General Purpose Input -  Output on the Arduino02   General Purpose Input -  Output on the Arduino
02 General Purpose Input - Output on the Arduino
 
Introduction to the Arduino
Introduction to the ArduinoIntroduction to the Arduino
Introduction to the Arduino
 
4.content mgmt
4.content mgmt4.content mgmt
4.content mgmt
 
8 Web Practices for Drupal
8  Web Practices for Drupal8  Web Practices for Drupal
8 Web Practices for Drupal
 
7 Theming in Drupal
7 Theming in Drupal7 Theming in Drupal
7 Theming in Drupal
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for Drupal
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

C game programming - SDL

  • 1. Workshop India Wilson Wingston Sharon wingston.sharon@gmail.com
  • 2. • 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.
  • 3. • 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.
  • 4. • 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.
  • 5.
  • 6. Code For the File that is currently Open in the tab Files that Belong to the Current project
  • 7. -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.
  • 8. • 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.
  • 9.
  • 10.
  • 11. • 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.
  • 12. • 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.
  • 13. The game itself Here the logic that specifies how the game behaves to the user Game Engine SDL 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
  • 14. • 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
  • 15. • This must be a global object. • Whatever we want SDL to display has to be written to this screen. •
  • 16.
  • 17.
  • 18.
  • 19. 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!
  • 20.
  • 21. Update Draw graphics game models elements Undraw Get input what is from not user required Check Game logic
  • 22. • 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!
  • 23.
  • 24.
  • 25.
  • 26. • 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? •?
  • 27. • 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()
  • 28. • 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.
  • 29.
  • 30. • 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.
  • 31.
  • 32. • 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?
  • 33. • 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
  • 34. • 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.
  • 35.
  • 36. • 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.
  • 37.
  • 38. • 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.
  • 39. • cls(0) fills the screen with black; • Updates point p1 with m1 for which direction to move the point • Putpixel to draw the point.
  • 40. • 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.
  • 41. • 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
  • 42.
  • 43.
  • 44. 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.
  • 45. • 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
  • 46.
  • 47.
  • 48. • 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!
  • 49. • 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.
  • 50. • 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,
  • 51.
  • 52.
  • 53. • 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
  • 54. • First we create a struct circle. (same like point except with radius too).
  • 55.
  • 56.
  • 57. • Same KEYUP/KEYDOW N movement code to move m1 and m2. • Adjust this code and get the second circle moving too.
  • 58. • 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.
  • 59. • 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.