Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
cmake_minimum_required(VERSION 3.11)

project(sdl2)
project(sdl-sprite)

add_executable(${PROJECT_NAME} main.c)

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)

target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)

target_link_libraries(${PROJECT_NAME} PRIVATE
${SDL2_LIBRARIES}
SDL3::SDL3
)

if(PSP)
# Create an EBOOT.PBP file
create_pbp_file(
TARGET ${PROJECT_NAME}
ICON_PATH NULL
Expand Down
85 changes: 85 additions & 0 deletions _includes/samples/sdl-sprite/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

int main(int argc, char *argv[]) {
// This prevents compiler warnings
// We don't actually need these variables, but they do need to be there so SDL_main works
(void)argc;
(void)argv;

// Initialize sdl
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return 1;
}

SDL_Window * window = NULL;
SDL_Renderer * renderer = NULL;
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
SDL_Quit();
return 2;
}

// Load the texture
SDL_Surface * pixels = SDL_LoadPNG("grass.png");
if (!pixels) {
SDL_Log("Couldn't load grass.png: %s", SDL_GetError());
SDL_Quit();
return 3;
}
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
SDL_DestroySurface(pixels);
if (!sprite) {
SDL_Log("Couldn't create texture: %s", SDL_GetError());
SDL_Quit();
return 4;
}

// Store the dimensions of the texture
SDL_FRect sprite_rect;
SDL_GetTextureSize(sprite, &sprite_rect.w, &sprite_rect.h);

// Set the position to draw to in the middle of the screen
sprite_rect.x = 480.0f / 2.0f - sprite_rect.w / 2.0f;
sprite_rect.y = 272.0f / 2.0f - sprite_rect.h / 2.0f;

int running = 1;
SDL_Event event;
while (running) {
// Process input
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
// End the loop if the programs is being closed
running = 0;
break;
case SDL_EVENT_GAMEPAD_ADDED:
// Connect a controller when it is connected
SDL_OpenGamepad(event.cdevice.which);
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
// Close the program if start is pressed
running = 0;
}
break;
}
}

// Clear the screen
SDL_RenderClear(renderer);

// Draw the 'grass' sprite
SDL_RenderTexture(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;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
cmake_minimum_required(VERSION 3.11)

project(sdl2_ttf)
project(sdl)

add_executable(${PROJECT_NAME} main.c)

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2_TTF REQUIRED SDL2_ttf)

target_include_directories(${PROJECT_NAME} PRIVATE
${SDL2_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIRS}
)
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)

target_link_libraries(${PROJECT_NAME} PRIVATE
${SDL2_LIBRARIES}
${SDL2_TTF_LIBRARIES}
SDL3::SDL3
)

if(PSP)
Expand Down
44 changes: 25 additions & 19 deletions _includes/samples/sdl2/main.c → _includes/samples/sdl/main.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
#include <SDL.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
int main(int argc, char *argv[]) {
// This prevents compiler warnings
// We don't actually need these variables, but they do need to be there so SDL_main works
(void)argc;
(void)argv;

SDL_Window * window = SDL_CreateWindow(
"window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
480,
272,
0
);
// Initialize sdl
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return 1;
}

SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Window * window = NULL;
SDL_Renderer * renderer = NULL;
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
SDL_Quit();
return 2;
}

SDL_Rect square = {216, 96, 34, 64};
SDL_FRect square = {216, 96, 34, 64};

int running = 1;
SDL_Event event;
while (running) {
// Process input
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
case SDL_EVENT_QUIT:
// End the loop if the programs is being closed
running = 0;
break;
case SDL_CONTROLLERDEVICEADDED:
case SDL_EVENT_GAMEPAD_ADDED:
// Connect a controller when it is connected
SDL_GameControllerOpen(event.cdevice.which);
SDL_OpenGamepad(event.cdevice.which);
break;
case SDL_CONTROLLERBUTTONDOWN:
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
// Close the program if start is pressed
running = 0;
}
Expand Down
73 changes: 0 additions & 73 deletions _includes/samples/sdl2_image/main.c

This file was deleted.

Loading
Loading