SlideShare a Scribd company logo
CHAPTER
FIFTYEIGHT
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.
58.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()
58.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()
538
Ring Documentation, Release 1.10
58.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)
58.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()
58.3. Switch between two images 539
Ring Documentation, Release 1.10
58.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()
58.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()
58.7 Display Transparent Images
Example:
58.5. Display PNG Images 540
Ring Documentation, Release 1.10
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()
58.8 Close Window Event
Example:
Load "libsdl.ring"
SDL_Init(SDL_INIT_EVERYTHING)
58.8. Close Window Event 541
Ring Documentation, Release 1.10
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()
58.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
58.9. Mouse Events 542
Ring Documentation, Release 1.10
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)
58.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()
58.10. Play Sound 543
Ring Documentation, Release 1.10
Mix_Quit()
SDL_DestroyWindow(win)
SDL_Quit()
58.10. Play Sound 544
CHAPTER
FIFTYNINE
USING RINGLIBUV
In this chapter we will learn about using RingLibuv
Note: To use RingLibuv, Check ring/extensions/ringlibuv folder.
Information from the library website: http://libuv.org/
Libuv is a multi-platform support library with a focus on asynchronous I/O.
Feature highlights
• Full-featured event loop backed by epoll, kqueue, IOCP, event ports.
• Asynchronous TCP and UDP sockets
• Asynchronous DNS resolution
• Asynchronous file and file system operations
• File system events
• ANSI escape code controlled TTY
• IPC with socket sharing, using Unix domain sockets or named pipes (Windows)
• Child processes
• Thread pool
• Signal handling
• High resolution clock
• Threading and synchronization primitives
59.1 First Application using RingLibuv
Example:
load "libuv.ring"
func main
myloop = new_uv_loop_t()
uv_loop_init(myloop)
? "Now quitting"
uv_run(myloop, UV_RUN_DEFAULT)
545
Ring Documentation, Release 1.10
uv_loop_close(myloop)
destroy_uv_loop_t(myloop)
Output:
Now quitting
59.2 The Events Loop
Example:
load "libuv.ring"
counter = 0
idler = NULL
func main
idler = new_uv_idle_t()
uv_idle_init(uv_default_loop(), idler)
uv_idle_start(idler, "wait()")
? "Idling..."
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
destroy_uv_idle_t(idler)
func wait
counter++
if counter >= 100000
uv_idle_stop(idler)
ok
Output:
Idling...
59.3 Server Example
Example:
load "libuv.ring"
? "Testing RingLibuv - Server Side"
DEFAULT_PORT = 13370
DEFAULT_BACKLOG = 1024
addr = new_sockaddr_in()
server = NULL
client = NULL
myloop = NULL
func main
myloop = uv_default_loop()
server = new_uv_tcp_t()
59.2. The Events Loop 546
Ring Documentation, Release 1.10
uv_tcp_init(myloop, server)
uv_ip4_addr("127.0.0.1", DEFAULT_PORT, addr)
uv_tcp_bind(server, addr, 0)
r = uv_listen(server, DEFAULT_BACKLOG, "newconnection()")
if r
? "Listen error " + uv_strerror(r)
return 1
ok
uv_run(myloop, UV_RUN_DEFAULT)
destroy_uv_tcp_t(server)
destroy_uv_sockaddr_in(addr)
func newconnection
? "New Connection"
aPara = uv_Eventpara(server,:connect)
nStatus = aPara[2]
if nStatus < 0
? "New connection error : " + nStatus
return
ok
client = new_uv_tcp_t()
uv_tcp_init(myloop, client)
if uv_accept(server, client) = 0
uv_read_start(client, uv_myalloccallback(), "echo_read()")
ok
func echo_read
aPara = uv_Eventpara(client,:read)
nRead = aPara[2]
buf = aPara[3]
if nRead > 0
req = new_uv_write_t()
wrbuf = uv_buf_init(get_uv_buf_t_base(buf), nread)
uv_write(req, client, wrbuf, 1, "echo_write()")
? uv_buf2str(wrbuf)
message = "message from the server to the client"
buf = new_uv_buf_t()
set_uv_buf_t_len(buf,len(message))
set_uv_buf_t_base(buf,varptr("message","char *"))
uv_write(req, client, buf, 1, "echo_write()")
ok
func echo_write
aPara = uv_Eventpara(client,:read)
req = aPara[1]
Output:
When we run the client, We will see the message “New Connection”
Then the message “hello from the client”
Testing RingLibuv - Server Side
New Connection
hello from the client
59.3. Server Example 547

More Related Content

PDF
The Ring programming language version 1.6 book - Part 50 of 189
PDF
The Ring programming language version 1.7 book - Part 51 of 196
PDF
The Ring programming language version 1.5.4 book - Part 48 of 185
PPTX
An Introduction to Deep Learning with Apache MXNet (November 2017)
PDF
The Ring programming language version 1.8 book - Part 53 of 202
PDF
Unity 13 space shooter game
PDF
Unity遊戲程式設計 - 2D Platformer遊戲
PDF
The Ring programming language version 1.4.1 book - Part 14 of 31
The Ring programming language version 1.6 book - Part 50 of 189
The Ring programming language version 1.7 book - Part 51 of 196
The Ring programming language version 1.5.4 book - Part 48 of 185
An Introduction to Deep Learning with Apache MXNet (November 2017)
The Ring programming language version 1.8 book - Part 53 of 202
Unity 13 space shooter game
Unity遊戲程式設計 - 2D Platformer遊戲
The Ring programming language version 1.4.1 book - Part 14 of 31

What's hot (20)

PDF
Unity遊戲程式設計(15) 實作Space shooter遊戲
PDF
The Ring programming language version 1.10 book - Part 70 of 212
PDF
Android game engine
PDF
The Ring programming language version 1.8 book - Part 59 of 202
PDF
A Development of Log-based Game AI using Deep Learning
PDF
The Ring programming language version 1.7 book - Part 64 of 196
PDF
The Ring programming language version 1.8 book - Part 56 of 202
PDF
【Unite Tokyo 2018】“100 Must-see Assets for 2018” by Virtual YouTuber, Cyber G...
TXT
2013 2-5
KEY
Introduction to Game Programming Tutorial
PPTX
Raspberry Pi à la GroovyFX
PDF
building_games_with_ruby_rubyconf
PDF
The Ring programming language version 1.5 book - Part 9 of 31
PDF
The Ring programming language version 1.9 book - Part 80 of 210
PDF
The Ring programming language version 1.7 book - Part 54 of 196
PDF
What is a Neural Network | Edureka
PDF
The Ring programming language version 1.8 book - Part 76 of 202
PDF
The Ring programming language version 1.2 book - Part 37 of 84
TXT
Game.log
PDF
The Ring programming language version 1.5.1 book - Part 48 of 180
Unity遊戲程式設計(15) 實作Space shooter遊戲
The Ring programming language version 1.10 book - Part 70 of 212
Android game engine
The Ring programming language version 1.8 book - Part 59 of 202
A Development of Log-based Game AI using Deep Learning
The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.8 book - Part 56 of 202
【Unite Tokyo 2018】“100 Must-see Assets for 2018” by Virtual YouTuber, Cyber G...
2013 2-5
Introduction to Game Programming Tutorial
Raspberry Pi à la GroovyFX
building_games_with_ruby_rubyconf
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.7 book - Part 54 of 196
What is a Neural Network | Edureka
The Ring programming language version 1.8 book - Part 76 of 202
The Ring programming language version 1.2 book - Part 37 of 84
Game.log
The Ring programming language version 1.5.1 book - Part 48 of 180
Ad

Similar to The Ring programming language version 1.10 book - Part 58 of 212 (20)

PDF
The Ring programming language version 1.5.1 book - Part 46 of 180
PDF
The Ring programming language version 1.2 book - Part 35 of 84
PDF
The Ring programming language version 1.5.3 book - Part 47 of 184
PDF
The Ring programming language version 1.5.3 book - Part 57 of 184
PDF
The Ring programming language version 1.3 book - Part 37 of 88
PDF
The Ring programming language version 1.5.2 book - Part 47 of 181
PDF
The Ring programming language version 1.5.4 book - Part 47 of 185
PDF
The Ring programming language version 1.9 book - Part 56 of 210
PDF
The Ring programming language version 1.5.3 book - Part 48 of 184
PDF
The Ring programming language version 1.5.3 book - Part 58 of 184
PDF
The Ring programming language version 1.3 book - Part 38 of 88
PDF
The Ring programming language version 1.6 book - Part 49 of 189
PDF
The Ring programming language version 1.5.2 book - Part 46 of 181
PDF
The Ring programming language version 1.5.1 book - Part 84 of 180
PDF
The Ring programming language version 1.4 book - Part 14 of 30
PDF
The Ring programming language version 1.9 book - Part 57 of 210
PDF
The Ring programming language version 1.9 book - Part 130 of 210
PDF
The Ring programming language version 1.9 book - Part 58 of 210
PDF
The Ring programming language version 1.8 book - Part 123 of 202
PDF
The Ring programming language version 1.3 book - Part 67 of 88
The Ring programming language version 1.5.1 book - Part 46 of 180
The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.5.3 book - Part 47 of 184
The Ring programming language version 1.5.3 book - Part 57 of 184
The Ring programming language version 1.3 book - Part 37 of 88
The Ring programming language version 1.5.2 book - Part 47 of 181
The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.6 book - Part 49 of 189
The Ring programming language version 1.5.2 book - Part 46 of 181
The Ring programming language version 1.5.1 book - Part 84 of 180
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.9 book - Part 57 of 210
The Ring programming language version 1.9 book - Part 130 of 210
The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.8 book - Part 123 of 202
The Ring programming language version 1.3 book - Part 67 of 88
Ad

More from Mahmoud Samir Fayed (20)

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

Recently uploaded (20)

PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
System and Network Administration Chapter 2
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
AI in Product Development-omnex systems
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
medical staffing services at VALiNTRY
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
How Creative Agencies Leverage Project Management Software.pdf
Digital Strategies for Manufacturing Companies
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administration Chapter 2
Odoo Companies in India – Driving Business Transformation.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
wealthsignaloriginal-com-DS-text-... (1).pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Wondershare Filmora 15 Crack With Activation Key [2025
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
2025 Textile ERP Trends: SAP, Odoo & Oracle
AI in Product Development-omnex systems
How to Migrate SBCGlobal Email to Yahoo Easily
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
medical staffing services at VALiNTRY
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Upgrade and Innovation Strategies for SAP ERP Customers

The Ring programming language version 1.10 book - Part 58 of 212

  • 1. CHAPTER FIFTYEIGHT 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. 58.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() 58.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() 538
  • 2. Ring Documentation, Release 1.10 58.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) 58.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() 58.3. Switch between two images 539
  • 3. Ring Documentation, Release 1.10 58.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() 58.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() 58.7 Display Transparent Images Example: 58.5. Display PNG Images 540
  • 4. Ring Documentation, Release 1.10 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() 58.8 Close Window Event Example: Load "libsdl.ring" SDL_Init(SDL_INIT_EVERYTHING) 58.8. Close Window Event 541
  • 5. Ring Documentation, Release 1.10 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() 58.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 58.9. Mouse Events 542
  • 6. Ring Documentation, Release 1.10 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) 58.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() 58.10. Play Sound 543
  • 7. Ring Documentation, Release 1.10 Mix_Quit() SDL_DestroyWindow(win) SDL_Quit() 58.10. Play Sound 544
  • 8. CHAPTER FIFTYNINE USING RINGLIBUV In this chapter we will learn about using RingLibuv Note: To use RingLibuv, Check ring/extensions/ringlibuv folder. Information from the library website: http://libuv.org/ Libuv is a multi-platform support library with a focus on asynchronous I/O. Feature highlights • Full-featured event loop backed by epoll, kqueue, IOCP, event ports. • Asynchronous TCP and UDP sockets • Asynchronous DNS resolution • Asynchronous file and file system operations • File system events • ANSI escape code controlled TTY • IPC with socket sharing, using Unix domain sockets or named pipes (Windows) • Child processes • Thread pool • Signal handling • High resolution clock • Threading and synchronization primitives 59.1 First Application using RingLibuv Example: load "libuv.ring" func main myloop = new_uv_loop_t() uv_loop_init(myloop) ? "Now quitting" uv_run(myloop, UV_RUN_DEFAULT) 545
  • 9. Ring Documentation, Release 1.10 uv_loop_close(myloop) destroy_uv_loop_t(myloop) Output: Now quitting 59.2 The Events Loop Example: load "libuv.ring" counter = 0 idler = NULL func main idler = new_uv_idle_t() uv_idle_init(uv_default_loop(), idler) uv_idle_start(idler, "wait()") ? "Idling..." uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); destroy_uv_idle_t(idler) func wait counter++ if counter >= 100000 uv_idle_stop(idler) ok Output: Idling... 59.3 Server Example Example: load "libuv.ring" ? "Testing RingLibuv - Server Side" DEFAULT_PORT = 13370 DEFAULT_BACKLOG = 1024 addr = new_sockaddr_in() server = NULL client = NULL myloop = NULL func main myloop = uv_default_loop() server = new_uv_tcp_t() 59.2. The Events Loop 546
  • 10. Ring Documentation, Release 1.10 uv_tcp_init(myloop, server) uv_ip4_addr("127.0.0.1", DEFAULT_PORT, addr) uv_tcp_bind(server, addr, 0) r = uv_listen(server, DEFAULT_BACKLOG, "newconnection()") if r ? "Listen error " + uv_strerror(r) return 1 ok uv_run(myloop, UV_RUN_DEFAULT) destroy_uv_tcp_t(server) destroy_uv_sockaddr_in(addr) func newconnection ? "New Connection" aPara = uv_Eventpara(server,:connect) nStatus = aPara[2] if nStatus < 0 ? "New connection error : " + nStatus return ok client = new_uv_tcp_t() uv_tcp_init(myloop, client) if uv_accept(server, client) = 0 uv_read_start(client, uv_myalloccallback(), "echo_read()") ok func echo_read aPara = uv_Eventpara(client,:read) nRead = aPara[2] buf = aPara[3] if nRead > 0 req = new_uv_write_t() wrbuf = uv_buf_init(get_uv_buf_t_base(buf), nread) uv_write(req, client, wrbuf, 1, "echo_write()") ? uv_buf2str(wrbuf) message = "message from the server to the client" buf = new_uv_buf_t() set_uv_buf_t_len(buf,len(message)) set_uv_buf_t_base(buf,varptr("message","char *")) uv_write(req, client, buf, 1, "echo_write()") ok func echo_write aPara = uv_Eventpara(client,:read) req = aPara[1] Output: When we run the client, We will see the message “New Connection” Then the message “hello from the client” Testing RingLibuv - Server Side New Connection hello from the client 59.3. Server Example 547