Simple animation in bc9Basic

Simple animation in sdl with bc9basic


$INCLUDE "Events.Bas"

Function WinMain()
'First we need to start up SDL, and make sure it went ok
If SDL_Init(SDL_INIT_EVERYTHING) != 0 Then
MsgBox("SDL_Init Error",SDL_GetError())
Function = 1
End If

'Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
Raw As SDL_Window Ptr win = SDL_CreateWindow("Display and Move a Sprint BC9BASIC and SDL RULES", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN)
'Make sure creating our window went ok
If win = NULL Then
MsgBox("SDL_CreateWindow Error:",SDL_GetError())
SDL_Quit()
Function = 1
End If

'Create a renderer that will draw to the window, -1 specifies that we want to load whichever
'video driver supports the flags we're passing
'Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
'SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
'synchornized with the monitor's refresh rate
Raw As SDL_Renderer Ptr ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)
If ren = NULL Then
SDL_DestroyWindow(win)
MsgBox("SDL_CreateRenderer Error: ", SDL_GetError())
SDL_Quit()
Function = 1
End If

'SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
'this lets us choose when to upload or remove textures from the GPU
Raw As SDL_Surface Ptr bmp = SDL_LoadBMP("sprite.bmp")
If bmp = NULL Then
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win);
MsgBox("SDL_LoadBMP Error",SDL_GetError())
SDL_Quit()
Function = 1
ELSE
SDL_SetColorKey(bmp, SDL_TRUE, SDL_MapRGB(bmp->format, 0xFF, 0x0, 0xFF))
End If


'To use a hardware accelerated texture for rendering we can create one from
'the surface we loaded
Raw As SDL_Texture Ptr tex = SDL_CreateTextureFromSurface(ren, bmp)
'We no longer need the surface
SDL_FreeSurface(bmp)
If tex = NULL Then
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
MsgBox("SDL_CreateTextureFromSurface Error:",SDL_GetError())
SDL_Quit()
Function = 1
End If

SDL_Event e
DIM quit AS BOOL
quit = FALSE


DIM ticks AS UINT
DIM seconds AS UINT
DIM sprite AS UINT

WHILE(NOT quit)

ticks   = SDL_GetTicks()
seconds = ticks / 1000
sprite  = seconds % 8

SDL_Rect srcrect
srcrect.x = sprite * 32
srcrect.y = 0
srcrect.h = 32
srcrect.w = 32

SDL_Rect r_dest
r_dest.x = 10
r_dest.y = 10
r_dest.h = 32
r_dest.w = 32

WHILE(SDL_PollEvent(&e))
if(e.type = SDL_QUIT) THEN
quit = TRUE
ELSEIF(e.type = SDL_KEYDOWN) THEN
SELECT CASE e.key.keysym.sym
CASE SDLK_UP
r_dest.y -= 4
IF(r_dest.y < 0) THEN
r_dest.y = 0
End If
CASE SDLK_DOWN
r_dest.y += 4
IF(r_dest.y > SCREEN_HEIGHT  - SPRITE_HEIGHT) THEN
r_dest.y = SCREEN_HEIGHT - SPRITE_HEIGHT
End If
CASE SDLK_LEFT
r_dest.x -= 4
IF(r_dest.x < 0) THEN
r_dest.x = 0
End If
CASE SDLK_RIGHT
r_dest.x +=4
IF(r_dest.x > SCREEN_WIDTH - SPRITE_WIDTH) THEN
r_dest.x = SCREEN_WIDTH - SPRITE_WIDTH
End If
END SELECT
End If
Wend
'Now lets draw our image
'First clear the renderer
SDL_SetRenderDrawColor(ren, 0,0,255,255)
SDL_RenderFillRect(ren, NULL)
SDL_RenderClear(ren)
'Draw the texture
SDL_RenderCopy(ren, tex, &srcrect, &r_dest)
'Update the screen
SDL_RenderPresent(ren)
WEND

'Clean up our objects and quit
SDL_DestroyTexture(tex)
SDL_DestroyRenderer(ren)
SDL_DestroyWindow(win)
SDL_Quit()
Function = 0
End Function

sprite
sprite
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s