Skip to content

Commit 05cf39e

Browse files
committed
Add sdl2_image example
1 parent ba0e6b6 commit 05cf39e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(sdl2-image)
4+
5+
add_executable(${PROJECT_NAME} main.c)
6+
7+
include(FindPkgConfig)
8+
pkg_search_module(SDL2 REQUIRED sdl2)
9+
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)
10+
11+
target_include_directories(${PROJECT_NAME} PRIVATE
12+
${SDL2_INCLUDE_DIRS}
13+
${SDL2_IMAGE_INCLUDE_DIRS}
14+
)
15+
16+
target_link_libraries(${PROJECT_NAME} PRIVATE
17+
${SDL2_LIBRARIES}
18+
${SDL2_IMAGE_LIBRARIES}
19+
)
20+
21+
if(PSP)
22+
# Create an EBOOT.PBP file
23+
create_pbp_file(
24+
TARGET ${PROJECT_NAME}
25+
ICON_PATH NULL
26+
BACKGROUND_PATH NULL
27+
PREVIEW_PATH NULL
28+
TITLE ${PROJECT_NAME}
29+
VERSION 01.00
30+
)
31+
endif()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <SDL.h>
2+
#include <SDL_image.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
7+
8+
// Enable png support for SDL2_image
9+
IMG_Init(IMG_INIT_PNG);
10+
11+
SDL_Window * window = SDL_CreateWindow(
12+
"window",
13+
SDL_WINDOWPOS_UNDEFINED,
14+
SDL_WINDOWPOS_UNDEFINED,
15+
480,
16+
272,
17+
0
18+
);
19+
20+
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
21+
22+
// Load the texture
23+
SDL_Surface * pixels = IMG_Load("grass.png");
24+
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
25+
SDL_FreeSurface(pixels);
26+
27+
// Store the dimensions of the texture
28+
SDL_Rect sprite_rect;
29+
SDL_QueryTexture(sprite, NULL, NULL, &sprite_rect.w, &sprite_rect.h);
30+
31+
// Set the position to draw to in the middle of the screen
32+
sprite_rect.x = 480/2 - sprite_rect.w/2;
33+
sprite_rect.y = 272/2 - sprite_rect.h/2;
34+
35+
int running = 1;
36+
SDL_Event event;
37+
while (running) {
38+
// Process input
39+
if (SDL_PollEvent(&event)) {
40+
switch (event.type) {
41+
case SDL_QUIT:
42+
// End the loop if the programs is being closed
43+
running = 0;
44+
break;
45+
case SDL_CONTROLLERDEVICEADDED:
46+
// Connect a controller when it is connected
47+
SDL_GameControllerOpen(event.cdevice.which);
48+
break;
49+
case SDL_CONTROLLERBUTTONDOWN:
50+
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
51+
// Close the program if start is pressed
52+
running = 0;
53+
}
54+
break;
55+
}
56+
}
57+
58+
// Clear the screen
59+
SDL_RenderClear(renderer);
60+
61+
// Draw a red square
62+
SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect);
63+
64+
// Draw everything on a white background
65+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
66+
SDL_RenderPresent(renderer);
67+
}
68+
SDL_DestroyRenderer(renderer);
69+
SDL_DestroyWindow(window);
70+
SDL_Quit();
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)