Skip to content

Commit

Permalink
Add sdl2_image example
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkwouter committed Sep 5, 2024
1 parent ba0e6b6 commit 05cf39e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
31 changes: 31 additions & 0 deletions _includes/samples/sdl2_image/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.5)

project(sdl2-image)

add_executable(${PROJECT_NAME} main.c)

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)

target_include_directories(${PROJECT_NAME} PRIVATE
${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME} PRIVATE
${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
)

if(PSP)
# Create an EBOOT.PBP file
create_pbp_file(
TARGET ${PROJECT_NAME}
ICON_PATH NULL
BACKGROUND_PATH NULL
PREVIEW_PATH NULL
TITLE ${PROJECT_NAME}
VERSION 01.00
)
endif()
73 changes: 73 additions & 0 deletions _includes/samples/sdl2_image/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 a red square
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;
}

0 comments on commit 05cf39e

Please sign in to comment.