much.love
21-11-2013, 06:22 PM
Hey guys.
I've just started with game development (and programming in general). Right now I'm just trying to learn the ropes and by reading some tutorials (most of them are for C or C++, sadly), I came up with this. But why doesn't the sprite move?
program sdltest;
uses
sdl2, SDL2_image;
const
SCREEN_WIDTH: integer = 640;
SCREEN_HEIGHT: integer = 480;
quit: boolean = false;
var
win: pSDL_WINDOW;
ren: pSDL_RENDERER;
background, sprite: pSDL_TEXTURE;
e: pSDL_EVENT;
ret: pSDL_RECT;
rect: pSDL_RECT;
thisTime, lastTime: integer;
dt: single;
begin
win := SDL_CreateWindow('Hello World', 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
ren := SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC);
background := IMG_LoadTexture(ren, 'background.png');
sprite := IMG_LoadTexture(ren, 'sprite.png');
new(ret);
ret^.x := 0;
ret^.y := 0;
ret^.w := 64;
ret^.h := 64;
new(rect);
rect^.x := 100;
rect^.y := 100;
rect^.w := 64;
rect^.h := 64;
new(e);
while quit = false do
begin
thisTime := SDL_GetTicks;
dt := (thisTime - lastTime) / 1000;
lastTime := thisTime;
while SDL_POLLEVENT(e) <> 0 do
begin
if e^.type_ = SDL_MOUSEBUTTONDOWN then
quit := TRUE;
if e^.type_ = SDL_KEYDOWN then
begin
case e^.type_ of
SDLK_LEFT: rect^.x := rect^.x - 10;
SDLK_RIGHT: rect^.x := rect^.x + 10;
SDLK_UP: rect^.y := rect^.y - 10;
SDLK_DOWN: rect^.y := rect^.y + 10;
end;
end;
SDL_RenderClear(ren);
SDL_RenderCopy(ren, background, nil, nil);
SDL_RenderCopy(ren, sprite, ret, rect);
SDL_RenderPresent(ren);
end;
end;
SDL_DestroyTexture(background);
SDL_DestroyTexture(sprite);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
IMG_QUIT;
SDL_QUIT;
end.
Also, if I add
if e^.type_ = SDL_QUIT then
quit := TRUE;
the program won't even compile.
Thanks a lot for your help in advance.
I've just started with game development (and programming in general). Right now I'm just trying to learn the ropes and by reading some tutorials (most of them are for C or C++, sadly), I came up with this. But why doesn't the sprite move?
program sdltest;
uses
sdl2, SDL2_image;
const
SCREEN_WIDTH: integer = 640;
SCREEN_HEIGHT: integer = 480;
quit: boolean = false;
var
win: pSDL_WINDOW;
ren: pSDL_RENDERER;
background, sprite: pSDL_TEXTURE;
e: pSDL_EVENT;
ret: pSDL_RECT;
rect: pSDL_RECT;
thisTime, lastTime: integer;
dt: single;
begin
win := SDL_CreateWindow('Hello World', 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
ren := SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC);
background := IMG_LoadTexture(ren, 'background.png');
sprite := IMG_LoadTexture(ren, 'sprite.png');
new(ret);
ret^.x := 0;
ret^.y := 0;
ret^.w := 64;
ret^.h := 64;
new(rect);
rect^.x := 100;
rect^.y := 100;
rect^.w := 64;
rect^.h := 64;
new(e);
while quit = false do
begin
thisTime := SDL_GetTicks;
dt := (thisTime - lastTime) / 1000;
lastTime := thisTime;
while SDL_POLLEVENT(e) <> 0 do
begin
if e^.type_ = SDL_MOUSEBUTTONDOWN then
quit := TRUE;
if e^.type_ = SDL_KEYDOWN then
begin
case e^.type_ of
SDLK_LEFT: rect^.x := rect^.x - 10;
SDLK_RIGHT: rect^.x := rect^.x + 10;
SDLK_UP: rect^.y := rect^.y - 10;
SDLK_DOWN: rect^.y := rect^.y + 10;
end;
end;
SDL_RenderClear(ren);
SDL_RenderCopy(ren, background, nil, nil);
SDL_RenderCopy(ren, sprite, ret, rect);
SDL_RenderPresent(ren);
end;
end;
SDL_DestroyTexture(background);
SDL_DestroyTexture(sprite);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
IMG_QUIT;
SDL_QUIT;
end.
Also, if I add
if e^.type_ = SDL_QUIT then
quit := TRUE;
the program won't even compile.
Thanks a lot for your help in advance.