SlideShare a Scribd company logo
Ring Documentation, Release 1.3
sdl_fillrect(surface,nullpointer(),0)
if sdl_get_sdl_event_motion_xrel(myevent) < 0
cMsg += " Left "
else
cMsg += " Right "
ok
if sdl_get_sdl_event_motion_yrel(myevent) < 0
cMsg += " Up "
else
cMsg += " Down "
ok
cMsg += " x = " + sdl_get_sdl_event_motion_x(myevent)
cMsg += " y = " + sdl_get_sdl_event_motion_y(myevent)
showmsg(cMsg)
off
end
SDL_Destroy_SDL_Color(Color)
TTF_CloseFont(font)
SDL_DestroyWindow(win)
SDL_Quit()
func showmsg mymsg
text = TTF_RenderText_Solid(font,mymsg,color)
SDL_BlitSurface(text, nullpointer(), surface, nullpointer())
SDL_UpdateWindowSurface(win)
SDL_FreeSurface(text)
46.10 Play Sound
Example:
Load "libsdl.ring"
SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT , 2, 10000)
Mix_AllocateChannels(4)
sound = Mix_LoadWav( "sound.wav" )
Mix_VolumeChunk(sound,1)
Mix_PlayChannel(1,sound,0)
myevent = sdl_new_sdl_event()
while true
thevent = sdl_pollevent(myevent)
switch sdl_get_sdl_event_type(myevent)
on sdl_get_sdl_quit()
exit
on sdl_get_sdl_keydown()
Key = SDL_GET_SDL_Event_key_keysym_sym(myevent)
if key = 27 exit ok
off
end
Mix_FreeChunk( sound )
Mix_CloseAudio()
46.10. Play Sound 348
Ring Documentation, Release 1.3
Mix_Quit()
SDL_DestroyWindow(win)
SDL_Quit()
46.10. Play Sound 349
CHAPTER
FORTYSEVEN
DEMO PROJECT - GAME ENGINE FOR 2D GAMES
In this chapter we will learn about using the different programming paradigms in the same project.
We will create a simple Game Engine for 2D Games.
You can use the Engine directly to create 2D Games for Desktop or Mobile.
47.1 Project Layers
The project contains the next layers
• Games Layer (Here we will use declarative programming)
• Game Engine Classes (Here we will use the Object-Oriented Programming paradigm)
• Interface to graphics library (Here we will use procedural programming)
• Graphics Library bindings (Here we have RingAllegro and RingLibSDL)
47.2 Graphics Library bindings
We already have RingAllegro to use the Allegro game programming library and we have RingLibSDL to use the
LibSDL game programming library.
Both of RingAllegro and RingLibSDL are created using the C language with the help of the Ring code generator for
extensions.
Each of them is over 10,000 lines of C code which is generated after writing simple configuration files (That are
processed by the code generator).
Each configuration file determines the functions names, structures information and constants then the generator process
this configuration file to produce the C code and the library that can be loaded from Ring code.
Using RingAllegro and RingLibSDL is very similar to using Allegro and LibSDL from C code where you have the
same functions but we can build on that using the Ring language features
• RingAllegro Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringallegro
• RingLibSDL Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringsdl
350
Ring Documentation, Release 1.3
47.3 Interface to graphics library
In this layer we have gl_allegro.ring and gl_libsdl.ring
Each library provides the same functions to be used with interacting with the Graphics Library.
This layer hides the details and the difference between RingAllegro and RingLibSDL.
You have the same functions, Just use it and you can switch between Allegro and LibSDL at anytime.
Why ?
Allegro is very simple, we can use it to quickly create 2D games for Windows, Linux and MacOS X.
In Ring 1.0 we started by supporting Allegro.
Also LibSDL is very powerful and popular, very easy to use for Mobile Development.
Ring 1.1 comes with support for LibSDL so we can quickly create games for Mobile.
Note: We can use just one library for Desktop and Mobile development.
• gl_allegro.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_allegro.ring
• gl_libsdl.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_libsdl.ring
47.4 Game Engine Classes
The Engine comes with the next classes
• GameBase class
• Resources class
• Game class
• GameObject class
• Sprite class
• Text class
• Animate class
• Sound class
• Map class
• Source Code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gameengine.ring
47.5 Games Layer
In this layer we create our games using the Game Engine classes
The classes are designed to be used through Declarative Programming.
In our games we will use the next classes
• Game class
• Sprite class
47.3. Interface to graphics library 351
Ring Documentation, Release 1.3
• Text class
• Animate class
• Sound class
• Map class
Note: Other classes in the engine are for internal use by the engine.
We will introduce some examples and three simple games :-
• Stars Fighter Game
• Flappy Bird 3000 Game
• Super Man 2016 Game
47.6 Game Class
The next table present the class attributes.
Attributes Description
FPS Number determines how many times the draw() method will be called per second.
FixedFPS Number determines how many times the animate() method will be called per second.
Title String determines the window title of the game.
aObjects List contains all objects in the game
shutdown True/False value to end the game loop
The next table present the class methods.
Method Description
refresh() Delete objects.
settitle(cTitle) Set the window title using a string parameter.
shutdown() Close the application.
The next table present a group of keywords defined by the class.
Keyword Description
sprite Create new Sprite object and add it to the game objects.
text Create new Text object and add it to the game objects.
animate Create new Animate object and add it to the game objects.
sound Create new Sound object and add it to the game objects.
map Create new Map object and add it ot the game objects.
47.7 GameObject Class
The next table present the class attributes.
47.6. Game Class 352
Ring Documentation, Release 1.3
Attributes Description
enabled True/False determine the state of the object (Active/Not Active)
x Number determine the x position of the object.
y Number determine the y position of the object.
width Number determine the width of the object.
height Number determine the height of the object.
nIndex Number determine the index of the object in objects list.
animate True/False to animate the object or not.
move True/False to move the object using the keyboard or not.
Scaled True/False to scale the object image or not.
draw Function to be called when drawing the object.
state Function to be called for object animation.
keypress Function to be called when a key is pressed.
mouse Function to be called when a mouse event happens.
The next table present the class methods.
Method Description
keyboard(oGame,nkey) Check Keyboard Events
mouse(oGame,nType,aMouseList) Check Mouse Events
rgb(r,g,b) Return new color using the RGB (Red, Green and Blue) Values.
47.8 Sprite Class
Parent Class : GameObject Class
The next table present the class attributes.
Attributes Description
image String determine the image file name.
point Number determine the limit of automatic movement of the object.
direction Number determine the direction of movement.
nstep Number determine the increment/decrement during movement.
type Number determine the object type in the game (Optional).
transparent True/False value determine if the image is transparent.
The next table present the class methods.
Method Description
Draw(oGame) Draw the object
47.9 Text Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
size Number determine the font size
font String determine the font file name
text String determine the text to be displayed
color Number determine the color
The next table present the class methods.
47.8. Sprite Class 353
Ring Documentation, Release 1.3
Method Description
Draw(oGame) Draw the object
47.10 Animate Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
frames Number determine the number of frames
frame Number determine the active frame
framewidth Number determine the frame width.
animate True/False determine using animate or not.
scaled True/False determine scaling image or not.
The next table present the class methods.
Method Description
Draw(oGame) Draw the object
47.11 Sound Class
Parent Class : GameObject Class
The next table present the class attributes.
Attributes Description
file String determine the sound file name.
once True/False determine to play the file one time or not (loop).
The next table present the class methods.
Method Description
playsound() Play the sound file
47.12 Map Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
aMap List determine the map content using numbers.
aImages List determine the image used for each number in the map.
BlockWidth Number determine the block width (default = 32).
BlockHeight Number determine the block height (default = 32).
Animate True/False determine the animation status.
The next table present the class methods.
Method Description
getvalue(x,y) Return the item value in the Map according to the visible part
47.10. Animate Class 354
Ring Documentation, Release 1.3
47.13 Using the Game Engine - Creating the Game Window
Load "gameengine.ring" # Give Control to the Game Engine
func main # Called by the Game Engine
oGame = New Game # Create the Game Object
{
title = "My First Game"
} # Start the Events Loop
Note: if you want to define global variables, this must be before load “gameengine.ring” because this instruction will
give the control to the game engine.
Screen Shot:
47.14 Using the Game Engine - Drawing Text
Load "gameengine.ring" # Give Control to the Game Engine
func main # Called by the Game Engine
47.13. Using the Game Engine - Creating the Game Window 355
Ring Documentation, Release 1.3
oGame = New Game # Create the Game Object
{
title = "My First Game"
text {
x = 10 y=50
animate = false
size = 20
file = "fonts/pirulen.ttf"
text = "game development using ring is very fun!"
color = rgb(0,0,0)
}
} # Start the Events Loop
Screen Shot:
47.15 Using the Game Engine - Moving Text
Load "gameengine.ring" # Give Control to the Game Engine
func main # Called by the Game Engine
oGame = New Game # Create the Game Object
47.15. Using the Game Engine - Moving Text 356
Ring Documentation, Release 1.3
{
title = "My First Game"
text {
x = 10 y=50
animate = false
size = 20
file = "fonts/pirulen.ttf"
text = "game development using ring is very fun!"
color = rgb(0,0,0) # Color = black
}
text {
x = 10 y=150
# Animation Part =====================================
animate = true # Use Animation
direction = GE_DIRECTION_INCVERTICAL # Increase y
point = 400 # Continue until y=400
nStep = 3 # Each time y+= 3
#=====================================================
size = 20
file = "fonts/pirulen.ttf"
text = "welcome to the real world!"
color = rgb(0,0,255) # Color = Blue
}
} # Start the Events Loop
Screen Shot:
47.15. Using the Game Engine - Moving Text 357

More Related Content

What's hot (19)

PDF
Lec 9 05_sept [compatibility mode]
Palak Sanghani
 
PDF
Creating Games for Asha - platform
Jussi Pohjolainen
 
PPTX
WP7 HUB_XNA
MICTT Palma
 
PDF
Intro to programming games with clojure
Juio Barros
 
KEY
Artdm170 week12 user_interaction
Gilbert Guerrero
 
PDF
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
PPTX
Maximizing performance of 3 d user generated assets in unity
WithTheBest
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
Peyton jones-2009-fun with-type_functions-slide
Takayuki Muranushi
 
PDF
Peyton jones-2011-type classes
Takayuki Muranushi
 
PDF
The Ring programming language version 1.2 book - Part 35 of 84
Mahmoud Samir Fayed
 
PPTX
WP7 HUB_XNA overview
MICTT Palma
 
PDF
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
PPTX
Procedural Content Generation with Clojure
Mike Anderson
 
PPTX
Enter The Matrix
Mike Anderson
 
PDF
Clojure for Data Science
henrygarner
 
PDF
maXbox starter68 machine learning VI
Max Kleiner
 
DOC
Learn Java 3D
Jay Thakkar
 
PDF
Extreme dxt compression
Ataceyhun Çelik
 
Lec 9 05_sept [compatibility mode]
Palak Sanghani
 
Creating Games for Asha - platform
Jussi Pohjolainen
 
WP7 HUB_XNA
MICTT Palma
 
Intro to programming games with clojure
Juio Barros
 
Artdm170 week12 user_interaction
Gilbert Guerrero
 
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
Maximizing performance of 3 d user generated assets in unity
WithTheBest
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
Peyton jones-2009-fun with-type_functions-slide
Takayuki Muranushi
 
Peyton jones-2011-type classes
Takayuki Muranushi
 
The Ring programming language version 1.2 book - Part 35 of 84
Mahmoud Samir Fayed
 
WP7 HUB_XNA overview
MICTT Palma
 
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Procedural Content Generation with Clojure
Mike Anderson
 
Enter The Matrix
Mike Anderson
 
Clojure for Data Science
henrygarner
 
maXbox starter68 machine learning VI
Max Kleiner
 
Learn Java 3D
Jay Thakkar
 
Extreme dxt compression
Ataceyhun Çelik
 

Similar to The Ring programming language version 1.3 book - Part 38 of 88 (20)

PDF
The Ring programming language version 1.5.3 book - Part 58 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 48 of 185
Mahmoud Samir Fayed
 
PPT
CS 354 Pixel Updating
Mark Kilgard
 
PDF
The Ring programming language version 1.5.1 book - Part 47 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 55 of 202
Mahmoud Samir Fayed
 
PPTX
ACM Mid-Southeast Slides
krinchan
 
PPTX
Bow&amp;arrow game
Shaibal Ahmed
 
PPT
Scmad Chapter07
Marcel Caraciolo
 
PDF
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
eyewaregallery
 
PDF
The Ring programming language version 1.9 book - Part 56 of 210
Mahmoud Samir Fayed
 
PDF
Introduction to Coding
Fabio506452
 
PPT
Tim Popl
mchaar
 
PPT
The Next Mainstream Programming Language: A Game Developer’s Perspective
guest4fd7a2
 
PDF
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 47 of 185
Mahmoud Samir Fayed
 
PPT
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio
 
DOCX
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
fredharris32
 
PPTX
Introduction to Deep Learning and Tensorflow
Oswald Campesato
 
PDF
R Markdown Reference Guide for reproducible research
danxexcel
 
PDF
C# Summer course - Lecture 2
mohamedsamyali
 
The Ring programming language version 1.5.3 book - Part 58 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 48 of 185
Mahmoud Samir Fayed
 
CS 354 Pixel Updating
Mark Kilgard
 
The Ring programming language version 1.5.1 book - Part 47 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 55 of 202
Mahmoud Samir Fayed
 
ACM Mid-Southeast Slides
krinchan
 
Bow&amp;arrow game
Shaibal Ahmed
 
Scmad Chapter07
Marcel Caraciolo
 
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
eyewaregallery
 
The Ring programming language version 1.9 book - Part 56 of 210
Mahmoud Samir Fayed
 
Introduction to Coding
Fabio506452
 
Tim Popl
mchaar
 
The Next Mainstream Programming Language: A Game Developer’s Perspective
guest4fd7a2
 
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 47 of 185
Mahmoud Samir Fayed
 
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
fredharris32
 
Introduction to Deep Learning and Tensorflow
Oswald Campesato
 
R Markdown Reference Guide for reproducible research
danxexcel
 
C# Summer course - Lecture 2
mohamedsamyali
 
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 

The Ring programming language version 1.3 book - Part 38 of 88

  • 1. Ring Documentation, Release 1.3 sdl_fillrect(surface,nullpointer(),0) if sdl_get_sdl_event_motion_xrel(myevent) < 0 cMsg += " Left " else cMsg += " Right " ok if sdl_get_sdl_event_motion_yrel(myevent) < 0 cMsg += " Up " else cMsg += " Down " ok cMsg += " x = " + sdl_get_sdl_event_motion_x(myevent) cMsg += " y = " + sdl_get_sdl_event_motion_y(myevent) showmsg(cMsg) off end SDL_Destroy_SDL_Color(Color) TTF_CloseFont(font) SDL_DestroyWindow(win) SDL_Quit() func showmsg mymsg text = TTF_RenderText_Solid(font,mymsg,color) SDL_BlitSurface(text, nullpointer(), surface, nullpointer()) SDL_UpdateWindowSurface(win) SDL_FreeSurface(text) 46.10 Play Sound Example: Load "libsdl.ring" SDL_Init(SDL_INIT_EVERYTHING) win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN) Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT , 2, 10000) Mix_AllocateChannels(4) sound = Mix_LoadWav( "sound.wav" ) Mix_VolumeChunk(sound,1) Mix_PlayChannel(1,sound,0) myevent = sdl_new_sdl_event() while true thevent = sdl_pollevent(myevent) switch sdl_get_sdl_event_type(myevent) on sdl_get_sdl_quit() exit on sdl_get_sdl_keydown() Key = SDL_GET_SDL_Event_key_keysym_sym(myevent) if key = 27 exit ok off end Mix_FreeChunk( sound ) Mix_CloseAudio() 46.10. Play Sound 348
  • 2. Ring Documentation, Release 1.3 Mix_Quit() SDL_DestroyWindow(win) SDL_Quit() 46.10. Play Sound 349
  • 3. CHAPTER FORTYSEVEN DEMO PROJECT - GAME ENGINE FOR 2D GAMES In this chapter we will learn about using the different programming paradigms in the same project. We will create a simple Game Engine for 2D Games. You can use the Engine directly to create 2D Games for Desktop or Mobile. 47.1 Project Layers The project contains the next layers • Games Layer (Here we will use declarative programming) • Game Engine Classes (Here we will use the Object-Oriented Programming paradigm) • Interface to graphics library (Here we will use procedural programming) • Graphics Library bindings (Here we have RingAllegro and RingLibSDL) 47.2 Graphics Library bindings We already have RingAllegro to use the Allegro game programming library and we have RingLibSDL to use the LibSDL game programming library. Both of RingAllegro and RingLibSDL are created using the C language with the help of the Ring code generator for extensions. Each of them is over 10,000 lines of C code which is generated after writing simple configuration files (That are processed by the code generator). Each configuration file determines the functions names, structures information and constants then the generator process this configuration file to produce the C code and the library that can be loaded from Ring code. Using RingAllegro and RingLibSDL is very similar to using Allegro and LibSDL from C code where you have the same functions but we can build on that using the Ring language features • RingAllegro Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringallegro • RingLibSDL Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringsdl 350
  • 4. Ring Documentation, Release 1.3 47.3 Interface to graphics library In this layer we have gl_allegro.ring and gl_libsdl.ring Each library provides the same functions to be used with interacting with the Graphics Library. This layer hides the details and the difference between RingAllegro and RingLibSDL. You have the same functions, Just use it and you can switch between Allegro and LibSDL at anytime. Why ? Allegro is very simple, we can use it to quickly create 2D games for Windows, Linux and MacOS X. In Ring 1.0 we started by supporting Allegro. Also LibSDL is very powerful and popular, very easy to use for Mobile Development. Ring 1.1 comes with support for LibSDL so we can quickly create games for Mobile. Note: We can use just one library for Desktop and Mobile development. • gl_allegro.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_allegro.ring • gl_libsdl.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_libsdl.ring 47.4 Game Engine Classes The Engine comes with the next classes • GameBase class • Resources class • Game class • GameObject class • Sprite class • Text class • Animate class • Sound class • Map class • Source Code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gameengine.ring 47.5 Games Layer In this layer we create our games using the Game Engine classes The classes are designed to be used through Declarative Programming. In our games we will use the next classes • Game class • Sprite class 47.3. Interface to graphics library 351
  • 5. Ring Documentation, Release 1.3 • Text class • Animate class • Sound class • Map class Note: Other classes in the engine are for internal use by the engine. We will introduce some examples and three simple games :- • Stars Fighter Game • Flappy Bird 3000 Game • Super Man 2016 Game 47.6 Game Class The next table present the class attributes. Attributes Description FPS Number determines how many times the draw() method will be called per second. FixedFPS Number determines how many times the animate() method will be called per second. Title String determines the window title of the game. aObjects List contains all objects in the game shutdown True/False value to end the game loop The next table present the class methods. Method Description refresh() Delete objects. settitle(cTitle) Set the window title using a string parameter. shutdown() Close the application. The next table present a group of keywords defined by the class. Keyword Description sprite Create new Sprite object and add it to the game objects. text Create new Text object and add it to the game objects. animate Create new Animate object and add it to the game objects. sound Create new Sound object and add it to the game objects. map Create new Map object and add it ot the game objects. 47.7 GameObject Class The next table present the class attributes. 47.6. Game Class 352
  • 6. Ring Documentation, Release 1.3 Attributes Description enabled True/False determine the state of the object (Active/Not Active) x Number determine the x position of the object. y Number determine the y position of the object. width Number determine the width of the object. height Number determine the height of the object. nIndex Number determine the index of the object in objects list. animate True/False to animate the object or not. move True/False to move the object using the keyboard or not. Scaled True/False to scale the object image or not. draw Function to be called when drawing the object. state Function to be called for object animation. keypress Function to be called when a key is pressed. mouse Function to be called when a mouse event happens. The next table present the class methods. Method Description keyboard(oGame,nkey) Check Keyboard Events mouse(oGame,nType,aMouseList) Check Mouse Events rgb(r,g,b) Return new color using the RGB (Red, Green and Blue) Values. 47.8 Sprite Class Parent Class : GameObject Class The next table present the class attributes. Attributes Description image String determine the image file name. point Number determine the limit of automatic movement of the object. direction Number determine the direction of movement. nstep Number determine the increment/decrement during movement. type Number determine the object type in the game (Optional). transparent True/False value determine if the image is transparent. The next table present the class methods. Method Description Draw(oGame) Draw the object 47.9 Text Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description size Number determine the font size font String determine the font file name text String determine the text to be displayed color Number determine the color The next table present the class methods. 47.8. Sprite Class 353
  • 7. Ring Documentation, Release 1.3 Method Description Draw(oGame) Draw the object 47.10 Animate Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description frames Number determine the number of frames frame Number determine the active frame framewidth Number determine the frame width. animate True/False determine using animate or not. scaled True/False determine scaling image or not. The next table present the class methods. Method Description Draw(oGame) Draw the object 47.11 Sound Class Parent Class : GameObject Class The next table present the class attributes. Attributes Description file String determine the sound file name. once True/False determine to play the file one time or not (loop). The next table present the class methods. Method Description playsound() Play the sound file 47.12 Map Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description aMap List determine the map content using numbers. aImages List determine the image used for each number in the map. BlockWidth Number determine the block width (default = 32). BlockHeight Number determine the block height (default = 32). Animate True/False determine the animation status. The next table present the class methods. Method Description getvalue(x,y) Return the item value in the Map according to the visible part 47.10. Animate Class 354
  • 8. Ring Documentation, Release 1.3 47.13 Using the Game Engine - Creating the Game Window Load "gameengine.ring" # Give Control to the Game Engine func main # Called by the Game Engine oGame = New Game # Create the Game Object { title = "My First Game" } # Start the Events Loop Note: if you want to define global variables, this must be before load “gameengine.ring” because this instruction will give the control to the game engine. Screen Shot: 47.14 Using the Game Engine - Drawing Text Load "gameengine.ring" # Give Control to the Game Engine func main # Called by the Game Engine 47.13. Using the Game Engine - Creating the Game Window 355
  • 9. Ring Documentation, Release 1.3 oGame = New Game # Create the Game Object { title = "My First Game" text { x = 10 y=50 animate = false size = 20 file = "fonts/pirulen.ttf" text = "game development using ring is very fun!" color = rgb(0,0,0) } } # Start the Events Loop Screen Shot: 47.15 Using the Game Engine - Moving Text Load "gameengine.ring" # Give Control to the Game Engine func main # Called by the Game Engine oGame = New Game # Create the Game Object 47.15. Using the Game Engine - Moving Text 356
  • 10. Ring Documentation, Release 1.3 { title = "My First Game" text { x = 10 y=50 animate = false size = 20 file = "fonts/pirulen.ttf" text = "game development using ring is very fun!" color = rgb(0,0,0) # Color = black } text { x = 10 y=150 # Animation Part ===================================== animate = true # Use Animation direction = GE_DIRECTION_INCVERTICAL # Increase y point = 400 # Continue until y=400 nStep = 3 # Each time y+= 3 #===================================================== size = 20 file = "fonts/pirulen.ttf" text = "welcome to the real world!" color = rgb(0,0,255) # Color = Blue } } # Start the Events Loop Screen Shot: 47.15. Using the Game Engine - Moving Text 357