1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #include "main.h"
void UpdateEvents(Input *in)
{
SDL_Event event;
in->mousebuttons[SDL_BUTTON_WHEELUP] = 0;
in->mousebuttons[SDL_BUTTON_WHEELDOWN] = 0;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
in->quit = 1;
break;
case SDL_KEYDOWN:
in->key[event.key.keysym.sym] = 1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym] = 0;
break;
case SDL_MOUSEMOTION:
in->mousex = event.motion.x;
in->mousey = event.motion.y;
in->mousexrel = event.motion.xrel;
in->mouseyrel = event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
in->mousebuttons[event.button.button] = 1;
break;
case SDL_MOUSEBUTTONUP:
if(event.button.button != SDL_BUTTON_WHEELDOWN && event.button.button != SDL_BUTTON_WHEELUP)
in->mousebuttons[event.button.button] = 0;
break;
default:
break;
}
}
}
SDL_Surface *LoadAdaptedIMG(const char IMG_Name[])
{
SDL_Surface *IMG = NULL;
SDL_Surface *IMG_Result = NULL;
if((IMG = IMG_Load(IMG_Name)) == NULL)
{
fprintf(stderr, "%s", IMG_GetError());
exit(-1);
}
IMG_Result = SDL_DisplayFormat(IMG);
SDL_FreeSurface(IMG);
return IMG_Result;
}
SDL_Surface *InitSDL(int width, int height, const char WindowsTitle[])
{
SDL_Surface *screen = NULL;
if(SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur lors de l'initialisation de la SDL : %s", SDL_GetError());
exit(-1);
}
if((screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
{
fprintf(stderr, "Erreur lors du chargement du mode video : %s", SDL_GetError());
exit(-1);
}
SDL_WM_SetCaption(WindowsTitle, NULL);
return screen;
}
int main(int argc, char* argv[])
{
Input in;
int width, height;
width = 640;
height = 480;
SDL_Surface *screen = InitSDL(width, height, "Scrolling");
SDL_Surface *picture = LoadAdaptedIMG("level.jpg");
char scrollSpeed = 16;
SDL_Rect camera;
memset(&in, 0, sizeof(in));
camera.x = 0;
camera.y = 0;
camera.w = width;
camera.h = height;
SDL_ShowCursor(SDL_DISABLE);
while(!in.key[SDLK_ESCAPE] && !in.quit)
{
UpdateEvents(&in);
if(in.key[SDLK_UP] && camera.y > 0)
{
camera.y -= scrollSpeed;
}
if(in.key[SDLK_DOWN] && camera.y < picture->h - camera.h)
{
camera.y += scrollSpeed;
}
if(in.key[SDLK_LEFT] && camera.x > 0)
{
camera.x -= scrollSpeed;
}
if(in.key[SDLK_RIGHT] && camera.x < picture->w - camera.w)
{
camera.x += scrollSpeed;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(picture, &camera, screen, NULL);
SDL_Flip(screen);
SDL_Delay(5);
}
SDL_FreeSurface(picture);
SDL_Quit();
return 0;
} |
Partager