Fellow coders,
I'm just getting into SDL and therefore I'm still at 'newbie' level 1.
So far, I have manged knocked up a tiny example which *should* display an image going around the screen in an arc using sin() and cos().
Only thing is, the image does not move. Any ideas?
Note: I have wrapped a few SDL functions here to make them easier for me to remember.
Main Code
The Blitz.h include:
I'm just getting into SDL and therefore I'm still at 'newbie' level 1.
So far, I have manged knocked up a tiny example which *should* display an image going around the screen in an arc using sin() and cos().
Only thing is, the image does not move. Any ideas?
Note: I have wrapped a few SDL functions here to make them easier for me to remember.
Main Code
Code:
#include <cmath>
#include "Blitz.h"
using namespace std;
// blitz example #1
int main(int argc, char *argv[]){
Graphics(320, 240, 0);
SetTitle("Load/Display image");
SDL_Surface *image=LoadBMP("testimage.bmp");
float v;
while (!AppClosed()){
Cls();
DrawImage(image,60+int(sin(v)*40.0),60+int(cos(v)*30.0));
v+=0.009;
Flip();
}
End();
}
The Blitz.h include:
Code:
#include <SDL/SDL.h>
// globals
SDL_Surface *screen; // main screen
int gfxW; // graphics width
int gfxH; // graphics height
Uint8 *inkeys;
// functions
void LockScreen(){
if (SDL_MUSTLOCK(screen)){
SDL_LockSurface(screen);
}
}
void UnLockScreen(){
if(SDL_MUSTLOCK(screen)){
SDL_UnlockSurface(screen);
}
}
void End(){
SDL_Quit();
}
bool AppClosed(){
SDL_Event event;
if(!SDL_PollEvent(&event)) return 0;
inkeys = SDL_GetKeyState(NULL);
if(inkeys[SDLK_ESCAPE]) return 1;
if(event.type == SDL_QUIT) return 1;
return 0;
}
void Graphics(int width, int height, bool full)
{
gfxW = width;
gfxH = height;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("Unable to init SDL: %s\n", SDL_GetError());
SDL_Quit();
}
if(!full==0){
screen = SDL_SetVideoMode(width,height,32,SDL_SWSURFACE|SDL_FULLSCREEN);
//LockScreen();
}
else{
screen = SDL_SetVideoMode(width,height,32,SDL_HWSURFACE|SDL_HWPALETTE);
}
if(screen == NULL)
{
printf("Unable to set video: %s\n", SDL_GetError());
SDL_Quit();
}
}
void SetTitle(char *text){
SDL_WM_SetCaption(text, NULL);
}
void Flip(){
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Delay(1);
}
void Cls(int r=0,int g=0,int b=0){
SDL_FillRect(screen, NULL, 65536 * r + 256 * g + b);
}
SDL_Surface *LoadBMP(char *file){
SDL_Surface *img = SDL_LoadBMP(file);
if ( img == NULL ){
fprintf(stderr, "Error loading BMP %s: %s\n", file, SDL_GetError());
}
return img;
}
void DrawImage(SDL_Surface *img,int x,int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = img->w;
dest.h = img->h;
SDL_BlitSurface(img, NULL, screen, &dest);
}