Skip to content

Instantly share code, notes, and snippets.

@ITotalJustice
Last active February 10, 2022 11:36
Show Gist options
  • Save ITotalJustice/8e9570b6dbfd8b075fa69dd9e1ce7445 to your computer and use it in GitHub Desktop.
Save ITotalJustice/8e9570b6dbfd8b075fa69dd9e1ce7445 to your computer and use it in GitHub Desktop.
psp sdl2 sample
cmake_minimum_required(VERSION 3.18.0)
project(TotalGB LANGUAGES C)
set(myname "TotalGB_SDL2")
add_executable(${myname} main.c)
option(UPSTREAM_SDL "use upstream sdl2" ON)
if (UPSTREAM_SDL)
include(FetchContent)
# log the git clone
Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(sdl2
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG main
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(sdl2)
target_link_libraries(${myname} LINK_PRIVATE
SDL2::SDL2-static
SDL2::SDL2main
)
else()
target_link_libraries(${myname} LINK_PRIVATE
SDL2::SDL2
SDL2::SDL2main
)
endif()
if (PSP)
target_link_libraries(${myname} LINK_PRIVATE
GL
pspvram
pspaudio
pspvfpu
pspdisplay
pspgu
pspge
pspdebug
psphprm
pspctrl
)
create_pbp_file(TARGET ${myname} # CMake executable target
TITLE "${CMAKE_PROJECT_NAME}" # displayed in game selection
# ICON_PATH
# BACKGROUND_PATH
# PREVIEW_PATH
)
endif()
#include <SDL.h>
#define WIDTH 320
#define HEIGHT 272
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
Uint32 pixel_format_enum = 0;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER);
window = SDL_CreateWindow("a", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 480, 272, SDL_WINDOW_FULLSCREEN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// PSP: BGR565 (4bpp)
pixel_format_enum = SDL_GetWindowPixelFormat(window);
// create texture of same window format so SDL doesn't
// have to do potential sw-side converting
texture = SDL_CreateTexture(renderer, pixel_format_enum, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
while (1)
{
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
// render a stripe, however this breaks on ppsspp
// PSP: pitch = 512 (so extra 192 byte of padding...odd)
void* pixels = NULL; int pitch = 0;
SDL_LockTexture(texture, NULL, &pixels, &pitch);
for (int i = 0; i < HEIGHT; i++)
{
// alternate between white and black on each scanline
const int colour = i&1?0xFF:0x00;
SDL_memset((uint8_t*)pixels + (i * pitch), colour, pitch);
}
SDL_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment