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
|
#include "screen.h"
SDL_Window *gWindow = NULL;
SDL_Surface *gScreenSurface = NULL;
SDL_Surface *ballSurface = NULL;
SDL_Surface *leftPaddleSurface = NULL;
SDL_Surface *rightPaddleSurface = NULL;
static const char * const SPRITE_PATH = "sprite.bmp";
static SDL_Surface *LoadSurface(const char * const path) {
SDL_Surface *optimizedSurface = NULL;
SDL_Surface * loadedSurface = SDL_LoadBMP(path);
if (loadedSurface == NULL) {
printf("Unable to load image %s! SDL Error: %s\n", path, SDL_GetError());
return NULL;
}
optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, 0);
if (optimizedSurface == NULL) {
printf("Unable to optimize image %s! SDL Error: %s\n", path, SDL_GetError());
return NULL;
}
SDL_FreeSurface(loadedSurface);
return optimizedSurface;
}
bool InitScreen(void) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
}
gWindow = SDL_CreateWindow( "Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (gWindow == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
gScreenSurface = SDL_GetWindowSurface(gWindow);
SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, 0x00, 0x00, 0x00));
ballSurface = LoadSurface(SPRITE_PATH);
leftPaddleSurface = LoadSurface(SPRITE_PATH);
rightPaddleSurface = LoadSurface(SPRITE_PATH);
return true;
}
void Close(void) {
SDL_FreeSurface(rightPaddleSurface);
rightPaddleSurface = NULL;
SDL_FreeSurface(leftPaddleSurface);
leftPaddleSurface = NULL;
SDL_FreeSurface(ballSurface);
ballSurface = NULL;
SDL_FreeSurface(gScreenSurface);
gScreenSurface = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
void Draw(const struct moveable_t m, SDL_Surface * const surface) {
SDL_Rect drect = {m.position.x, m.position.y, m.width, m.height};
const int status = SDL_BlitScaled(surface, NULL, gScreenSurface, &drect);
if (status < 0) {
printf("Error with draw! SDL Error: %s\n", SDL_GetError());
}
}
|