Вы находитесь на странице: 1из 10

CHAPTER

FIFTYONE

USING RINGLIBSDL

In this chapter we will learn about using RingLibSDL to create games based on the LibSDL, SDLImage, SDLTTF and
SDLMixer libraries.

Tip: RingLibSDL is not distributed with the binary releases for desktop which uses RingAllegro

Note: To use RingLibSDL, Check ring/android/ringlibsdl folder.

51.1 Create Window

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
SDL_Delay(2000)
SDL_DestroyWindow(win)
SDL_Quit()

51.2 Display Image

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )
bmp = SDL_LoadBMP("hello.bmp")
tex = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)
SDL_RenderClear(ren)
SDL_RenderCopy2(ren,tex)
SDL_RenderPresent(ren)
SDL_Delay(2000)
SDL_DestroyTexture(tex)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

425
Ring Documentation, Release 1.5.1

51.3 Switch between two images

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )
bmp = SDL_LoadBMP("hello.bmp")
tex = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)
bmp = SDL_LoadBMP("hello2.bmp")
tex2 = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)

for x = 1 to 10 showtex(tex) showtex(tex2) next

SDL_DestroyTexture(tex)
SDL_DestroyTexture(tex2)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

func showtex oTex


SDL_RenderClear(ren)
SDL_RenderCopy2(ren,oTex)
SDL_RenderPresent(ren)
SDL_Delay(200)

51.4 Draw Rectangle

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )
SDL_RenderClear(ren)
rect = sdl_new_sdl_rect()
sdl_set_sdl_rect_x(rect,10)
sdl_set_sdl_rect_y(rect,10)
sdl_set_sdl_rect_w(rect,100)
sdl_set_sdl_rect_h(rect,100)
SDL_SetRenderDrawColor(ren,255,255,255,255)
SDL_RenderDrawRect(ren,rect)
sdl_destroy_sdl_rect(rect)
SDL_RenderPresent(ren)
SDL_Delay(2000)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

51.3. Switch between two images 426


Ring Documentation, Release 1.5.1

51.5 Display PNG Images

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )
bmp = IMG_Load("hello3.png")
tex = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)
SDL_RenderClear(ren)
SDL_RenderCopy2(ren,tex)
SDL_RenderPresent(ren)
SDL_Delay(2000)
SDL_DestroyTexture(tex)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

51.6 Use TTF Fonts

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )
SDL_RenderClear(ren)

TTF_Init()
font = TTF_OpenFont("pirulen.ttf", 16)
color = sdl_new_sdl_color()
sdl_set_sdl_color_r(color,0)
sdl_set_sdl_color_g(color,255)
sdl_set_sdl_color_b(color,0)
text = TTF_RenderText_Solid(font,"Welcome to the Ring language",color)
surface = SDL_GetWindowSurface(win)
SDL_BlitSurface(text, nullpointer(), surface, nullpointer())
SDL_UpdateWindowSurface(win)
SDL_Delay(2000)

SDL_Destroy_SDL_Color(color)
SDL_FreeSurface(text)
TTF_CloseFont(font)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

51.7 Display Transparent Images

Example:

51.5. Display PNG Images 427


Ring Documentation, Release 1.5.1

Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)

flags = IMG_INIT_JPG | IMG_INIT_PNG


IMG_Init(flags)

win = SDL_CreateWindow("Hello World!", 100, 100, 800, 600, SDL_WINDOW_SHOWN)


ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )

bmp = IMG_Load("stars.jpg")
tex = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)
SDL_RenderClear(ren)
SDL_RenderCopy(ren,tex,nullpointer(),nullpointer())
SDL_DestroyTexture(tex)

bmp = IMG_Load("player.png")
# Image - Set Transparent color (white)
myformat = sdl_get_sdl_surface_format(bmp)
white = SDL_MapRGB(myformat, 255, 255, 255)
SDL_SetColorKey(bmp, SDL_True, white)

tex = SDL_CreateTextureFromSurface(ren,bmp)
SDL_FreeSurface(bmp)
rect = sdl_new_sdl_rect()
sdl_set_sdl_rect_x(rect,0)
sdl_set_sdl_rect_y(rect,0)
sdl_set_sdl_rect_w(rect,100)
sdl_set_sdl_rect_h(rect,100)
SDL_RenderCopy(ren,tex,nullpointer(),rect)

SDL_SetTextureBlendMode(tex,2)
SDL_SetTextureAlphaMod(tex,255)
sdl_set_sdl_rect_x(rect,200)
sdl_set_sdl_rect_y(rect,200)
sdl_set_sdl_rect_w(rect,100)
sdl_set_sdl_rect_h(rect,100)
SDL_RenderCopy(ren,tex,nullpointer(),rect)

SDL_DestroyTexture(tex)
SDL_Destroy_SDL_Rect(rect)

SDL_RenderPresent(ren)
SDL_Delay(2000)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()

51.8 Close Window Event

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)

51.8. Close Window Event 428


Ring Documentation, Release 1.5.1

win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)

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

SDL_DestroyWindow(win)
SDL_Quit()

51.9 Mouse Events

Example:
Load "libsdl.ring"

SDL_Init(SDL_INIT_EVERYTHING)

win = SDL_CreateWindow("Mouse Events ", 100, 100, 640, 480, SDL_WINDOW_SHOWN)

TTF_Init()
font = TTF_OpenFont("pirulen.ttf", 16)
color = sdl_new_sdl_color()
sdl_set_sdl_color_r(color,0)
sdl_set_sdl_color_g(color,255)
sdl_set_sdl_color_b(color,0)

surface = SDL_GetWindowSurface(win)

myevent = sdl_new_sdl_event()
while true
cMsg = ""
sdl_pollevent(myevent)
switch sdl_get_sdl_event_type(myevent)
on SDL_QUIT
exit
on SDL_KEYDOWN
Key = SDL_GET_SDL_Event_key_keysym_sym(myevent)
if key = 27 exit ok
on SDL_MOUSEBUTTONDOWN
if sdl_get_Sdl_Event_button_button(myevent) = SDL_BUTTON_LEFT
SDL_SETWINDOWTITLE(win, " Button_Left_Down " )
but sdl_get_Sdl_Event_button_button(myevent) = SDL_BUTTON_MIDDLE
SDL_SETWINDOWTITLE(win, " Button_Middle_Down " )
but sdl_get_Sdl_Event_button_button(myevent) = SDL_BUTTON_RIGHT
SDL_SETWINDOWTITLE(win, " Button_Right_Down " )
ok
on SDL_MOUSEMOTION

51.9. Mouse Events 429


Ring Documentation, Release 1.5.1

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)

51.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()

51.10. Play Sound 430


Ring Documentation, Release 1.5.1

Mix_Quit()
SDL_DestroyWindow(win)
SDL_Quit()

51.10. Play Sound 431


CHAPTER

FIFTYTWO

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.

52.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)

52.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

432
Ring Documentation, Release 1.5.1

52.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

52.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

52.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

52.3. Interface to graphics library 433


Ring Documentation, Release 1.5.1

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

52.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.

52.7 GameObject Class

The next table present the class attributes.

52.6. Game Class 434

Вам также может понравиться