Last active
January 30, 2022 06:31
-
-
Save xenobrain/df630ce9a47a14a09fd7cdd2e5244032 to your computer and use it in GitHub Desktop.
CMakeRC + stb_image + SDL_Renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.21) | |
include(CMakeRC.cmake) | |
project(test) | |
file(GLOB_RECURSE ENGINE_SOURCES "${CMAKE_SOURCE_DIR}/engine/*.cpp") | |
file(GLOB_RECURSE GAME_SOURCES "${CMAKE_SOURCE_DIR}/game/*.cpp") | |
# Resource compiler | |
file(GLOB_RECURSE ASSETS "${CMAKE_SOURCE_DIR}/assets/*.*") | |
cmrc_add_resource_library(game-resources ALIAS game::resources NAMESPACE resources ${ASSETS}) | |
set(CLANG_WARNINGS | |
-Wall | |
-Wextra | |
-Wshadow | |
-Wnon-virtual-dtor | |
-Wold-style-cast | |
-Wcast-align | |
-Wunused | |
-Woverloaded-virtual | |
-Wpedantic | |
-Wconversion | |
-Wsign-conversion | |
-Wnull-dereference | |
-Wdouble-promotion | |
-Wformat=2 | |
-Wimplicit-fallthrough) | |
set(CLANG_SANITIZERS -fsanitize=address -fno-omit-frame-pointer) | |
find_package(SDL2 REQUIRED) | |
add_executable(${PROJECT_NAME} ${GAME_SOURCES} ${ENGINE_SOURCES}) | |
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) | |
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/engine) | |
target_compile_definitions(${PROJECT_NAME} PRIVATE PLATFORM_SDL=1 RENDERER_SDL=1) | |
target_compile_options(${PROJECT_NAME} PRIVATE ${CLANG_WARNINGS} ${CLANG_SANITIZERS}) | |
# Statically link resource library | |
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2 SDL2::SDL2main SDL2::SDL2-static game::resources ${CLANG_SANITIZERS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <SDL.h> | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
#include <cmrc/cmrc.hpp> | |
CMRC_DECLARE(resources); | |
auto constexpr PLAYER_TEXTURE = "assets/spaceship.png"; | |
SDL_Window* window; | |
SDL_Renderer* renderer; | |
auto main(int, char**) -> int { | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_CreateWindowAndRenderer(1280, 720, {}, &window, &renderer); | |
auto fs = cmrc::resources::get_filesystem(); | |
auto tex = fs.open(PLAYER_TEXTURE); | |
auto tex_vec = std::vector<unsigned char>(tex.begin(), tex.end()); | |
auto width = 0, height = 0, source_format = 0; | |
auto* image = stbi_load_from_memory(tex_vec.data(), tex_vec.size(), &width, &height, &source_format, STBI_rgb_alpha); | |
auto* surface = SDL_CreateRGBSurfaceWithFormatFrom(image, width, height, 32, width * 4, SDL_PIXELFORMAT_RGBA32); | |
auto* texture = SDL_CreateTextureFromSurface(renderer, surface); | |
// free surface and image | |
auto running = true; | |
while (running) { | |
auto event = SDL_Event{}; | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) running = false; | |
} | |
SDL_RenderClear(renderer); | |
auto source_rect = SDL_Rect{0, 0, width, height}; | |
auto destination_rect = SDL_Rect{10, 20, width, height}; | |
SDL_RenderCopy(renderer, texture, &source_rect, &destination_rect); | |
SDL_RenderPresent(renderer); | |
} | |
// destroy texture, renderer, window, sdl_quit | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment