-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.c
More file actions
73 lines (61 loc) · 2.13 KB
/
Copy pathmain.c
File metadata and controls
73 lines (61 loc) · 2.13 KB
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
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
// Enable png support for SDL2_image
IMG_Init(IMG_INIT_PNG);
SDL_Window * window = SDL_CreateWindow(
"window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
480,
272,
0
);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Load the texture
SDL_Surface * pixels = IMG_Load("grass.png");
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
SDL_FreeSurface(pixels);
// Store the dimensions of the texture
SDL_Rect sprite_rect;
SDL_QueryTexture(sprite, NULL, NULL, &sprite_rect.w, &sprite_rect.h);
// Set the position to draw to in the middle of the screen
sprite_rect.x = 480/2 - sprite_rect.w/2;
sprite_rect.y = 272/2 - sprite_rect.h/2;
int running = 1;
SDL_Event event;
while (running) {
// Process input
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
// End the loop if the programs is being closed
running = 0;
break;
case SDL_CONTROLLERDEVICEADDED:
// Connect a controller when it is connected
SDL_GameControllerOpen(event.cdevice.which);
break;
case SDL_CONTROLLERBUTTONDOWN:
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
// Close the program if start is pressed
running = 0;
}
break;
}
}
// Clear the screen
SDL_RenderClear(renderer);
// Draw the 'grass' sprite
SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect);
// Draw everything on a white background
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}