Skip to content

Commit

Permalink
Updated SDL3, SDL_Image, SDL_TTF, freetype and ImGUI/
Browse files Browse the repository at this point in the history
  • Loading branch information
bXi committed Jul 23, 2024
1 parent f946256 commit aa6b9f6
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 53 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ set(INTERFACE_SDL3_SHARED OFF)
set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS ON)
CPMAddPackage(
NAME SDL3
GIT_TAG e3395a7
GIT_TAG f5f44de
GITHUB_REPOSITORY libsdl-org/SDL
)

Expand All @@ -117,7 +117,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)

CPMAddPackage(
NAME SDL3_image
GIT_TAG 9add12c
GIT_TAG 8abc07d
GITHUB_REPOSITORY libsdl-org/SDL_image
)

Expand All @@ -129,8 +129,8 @@ if(NOT MSVC)
CPMAddPackage(
NAME freetype
GIT_REPOSITORY https://github.com/aseprite/freetype2.git
GIT_TAG e8ebfe9
)
GIT_TAG 9a2d6d9
)

if (freetype_ADDED)
add_library(Freetype::Freetype ALIAS freetype)
Expand All @@ -140,7 +140,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)

CPMAddPackage(
NAME SDL3_ttf
GIT_TAG 6e260a2
GIT_TAG 5b6171c
GITHUB_REPOSITORY libsdl-org/SDL_ttf
)

Expand All @@ -157,7 +157,7 @@ target_link_libraries("luminoveau" PUBLIC
if (ADD_IMGUI)
CPMAddPackage(
NAME Imgui
GIT_TAG 4f9ba19
GIT_TAG fe09ebb
GITHUB_REPOSITORY ocornut/imgui
)

Expand Down
21 changes: 15 additions & 6 deletions assethandler/assethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ TextureAsset AssetHandler::_loadTexture(const std::string &fileName) {
SDL_Texture *tex = SDL_CreateTextureFromSurface(Window::GetRenderer(), surface);
texture.texture = tex;

SDL_PropertiesID props = SDL_GetTextureProperties(tex);

texture.id = props[SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER];


SDL_SetTextureScaleMode(tex, (SDL_ScaleMode)defaultMode);

Expand Down Expand Up @@ -67,6 +71,9 @@ TextureAsset AssetHandler::_createEmptyTexture(const vf2d &size) {

texture.texture = SDL_CreateTexture(Window::GetRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, (int) size.x, (int) size.y);

SDL_PropertiesID props = SDL_GetTextureProperties(texture.texture);
texture.id = props[SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER];

SDL_SetTextureScaleMode(texture.texture, (SDL_ScaleMode)defaultMode);

return texture;
Expand All @@ -77,9 +84,11 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {
SDL_Surface *surface = texture.surface;

if (!surface) {
int texWidth, texHeight;
SDL_QueryTexture(texture.texture, NULL, NULL, &texWidth, &texHeight);
surface = SDL_CreateSurface(texWidth, texHeight,SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);
float texWidth, texHeight;

SDL_PropertiesID props = SDL_GetTextureProperties(texture.texture);
SDL_GetTextureSize(texture.texture, &texWidth, &texHeight);
surface = SDL_CreateSurface((int)texWidth, (int)texHeight, SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32);

if (!surface) {
SDL_Log("%s", SDL_GetError());
Expand All @@ -91,8 +100,8 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {

SDL_Surface *rgbaSurface = surface;

if (surface->format->format != SDL_PIXELFORMAT_RGBA32) {
rgbaSurface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32);
if (surface->format != SDL_PIXELFORMAT_RGBA32) {
rgbaSurface = SDL_ConvertSurface(surface, SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32);
}

unsigned char *pixels = new unsigned char[rgbaSurface->w * rgbaSurface->h * 4];
Expand All @@ -105,7 +114,7 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {

delete[] pixels;

if (surface->format->format != SDL_PIXELFORMAT_RGBA32) {
if (surface->format != SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32) {
SDL_DestroySurface(rgbaSurface);
}
}
Expand Down
10 changes: 5 additions & 5 deletions input/inputdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ class InputDevice {
int gamepadID;

std::map<Buttons, std::list<int>> mappingKB = {
{Buttons::LEFT, {SDLK_a, SDLK_LEFT}},
{Buttons::RIGHT, {SDLK_d, SDLK_RIGHT}},
{Buttons::UP, {SDLK_w, SDLK_UP}},
{Buttons::DOWN, {SDLK_s, SDLK_DOWN}},
{Buttons::LEFT, {SDLK_A, SDLK_LEFT}},
{Buttons::RIGHT, {SDLK_D, SDLK_RIGHT}},
{Buttons::UP, {SDLK_W, SDLK_UP}},
{Buttons::DOWN, {SDLK_S, SDLK_DOWN}},

{Buttons::ACCEPT, {SDLK_SPACE, SDLK_KP_ENTER, SDLK_RETURN}},
{Buttons::BACK, {SDLK_ESCAPE, SDLK_BACKSPACE}},

{Buttons::SWITCH_NEXT, {SDLK_TAB}},
{Buttons::SWITCH_PREV, {SDLK_BACKQUOTE}},
{Buttons::SWITCH_PREV, {SDLK_GRAVE}},

{Buttons::RUN, {SDL_SCANCODE_LSHIFT}},

Expand Down
6 changes: 3 additions & 3 deletions input/inputhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool Input::_keyPressed(int key) {
auto curState = currentKeyboardState;
auto prevState = previousKeyboardState;

auto scancode = SDL_GetScancodeFromKey(key);
auto scancode = SDL_GetScancodeFromKey(key, nullptr);

return curState[scancode] == 1 && prevState[scancode] == 0;
}
Expand All @@ -97,14 +97,14 @@ bool Input::_keyReleased(int key) {
auto curState = currentKeyboardState;
auto prevState = previousKeyboardState;

auto scancode = SDL_GetScancodeFromKey(key);
auto scancode = SDL_GetScancodeFromKey(key, nullptr);

return curState[scancode] == 0 && prevState[scancode] == 1;
}

bool Input::_keyDown(int key) {

auto scancode = SDL_GetScancodeFromKey(key);
auto scancode = SDL_GetScancodeFromKey(key, nullptr);

return currentKeyboardState[scancode] == 1;
}
Expand Down
2 changes: 1 addition & 1 deletion input/inputhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Input {
Uint32 previousMouseButtons = 0;


SDL_JoystickID *joystickIds = nullptr;
const SDL_JoystickID *joystickIds = nullptr;

struct gamepadInfo {
SDL_JoystickID joystickId;
Expand Down
2 changes: 1 addition & 1 deletion render2d/SDL2_gfxPrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Andreas Schiffler -- aschiffler at ferzkopp dot net
#define M_PI 3.1415926535897932384626433832795
#endif

#include "SDL.h"
#include "SDL3/SDL.h"

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
Expand Down
39 changes: 14 additions & 25 deletions render2d/SDL2_rotozoom.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns)
return NULL;
}

if ((src->format->bits_per_pixel % 8) != 0) {
if (src->format != 0) {
SDL_SetError("Invalid source surface bit depth");
return NULL;
}
Expand All @@ -836,7 +836,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns)
newHeight = src->h;
}

dst = SDL_CreateSurface(newWidth, newHeight, src->format->bits_per_pixel);
dst = SDL_CreateSurface(newWidth, newHeight, src->format);
if(!dst) {
SDL_SetError("Could not create destination surface");
return NULL;
Expand All @@ -850,7 +850,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns)
}

/* Calculate byte-per-pixel */
bpp = src->format->bits_per_pixel / 8;
bpp = SDL_GetPixelFormatDetails(src->format)->bits_per_pixel;

switch(normalizedClockwiseTurns) {
case 0: /* Make a copy of the surface */
Expand Down Expand Up @@ -1070,8 +1070,8 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do
/*
* Determine if source surface is 32bit or 8bit
*/
is32bit = (src->format->bits_per_pixel == 32);
if ((is32bit) || (src->format->bits_per_pixel == 8)) {
is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32);
if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) {
/*
* Use source surface 'as is'
*/
Expand Down Expand Up @@ -1174,10 +1174,7 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do
/*
* Copy palette and colorkey info
*/
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;

/*
* Call the 8bit transformation routine to do the rotation
*/
Expand Down Expand Up @@ -1250,10 +1247,7 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do
/*
* Copy palette and colorkey info
*/
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;


/*
* Call the 8bit transformation routine to do the zooming
Expand Down Expand Up @@ -1362,8 +1356,8 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo
/*
* Determine if source surface is 32bit or 8bit
*/
is32bit = (src->format->bits_per_pixel == 32);
if ((is32bit) || (src->format->bits_per_pixel == 8)) {
is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32);
if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) {
/*
* Use source surface 'as is'
*/
Expand Down Expand Up @@ -1441,10 +1435,7 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo
/*
* Copy palette and colorkey info
*/
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;

/*
* Call the 8bit transformation routine to do the zooming
*/
Expand Down Expand Up @@ -1507,8 +1498,8 @@ SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory)
/*
* Determine if source surface is 32bit or 8bit
*/
is32bit = (src->format->bits_per_pixel == 32);
if ((is32bit) || (src->format->bits_per_pixel == 8)) {
is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32);
if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) {
/*
* Use source surface 'as is'
*/
Expand Down Expand Up @@ -1587,10 +1578,8 @@ SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory)
/*
* Copy palette and colorkey info
*/
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;


/*
* Call the 8bit transformation routine to do the shrinking
*/
Expand Down
2 changes: 1 addition & 1 deletion render2d/SDL2_rotozoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
#define M_PI 3.1415926535897932384626433832795
#endif

#include "SDL.h"
#include "SDL3/SDL.h"

/* ---- Defines */

Expand Down
17 changes: 12 additions & 5 deletions window/windowhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ void Window::_initWindow(const std::string &title, int width, int height, int sc
height *= scale;
}

SDL_InitSubSystem(SDL_InitFlags::SDL_INIT_VIDEO);
SDL_InitSubSystem(SDL_INIT_VIDEO);
auto window = SDL_CreateWindow(title.c_str(), width, height, flags);
if (window) {
m_window.reset(window);
} else {
throw std::runtime_error(SDL_GetError());
}

SDL_PropertiesID props;

props = SDL_CreateProperties();

SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
#ifdef __EMSCRIPTEN__
auto renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetStringProperty(props, SDL_PROP_RENDERER_VSYNC_NUMBER, 1);
#else
auto renderer = SDL_CreateRenderer(window, "opengl", flags);
SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, "opengl");
#endif

auto renderer = SDL_CreateRendererWithProperties(props);

if (!renderer) {
SDL_Log("SDL renderer failed: %s", SDL_GetError());
SDL_Log("Showing available SDL Renderers:");
Expand Down Expand Up @@ -153,10 +160,10 @@ void Window::_handleInput() {
_shouldQuit = true;
break;
case SDL_EventType::SDL_EVENT_KEY_DOWN:
newKeysDown.push_back(event.key.keysym.scancode);
newKeysDown.push_back(event.key.scancode);
break;
case SDL_EventType::SDL_EVENT_KEY_UP:
newKeysUp.push_back(event.key.keysym.scancode);
newKeysUp.push_back(event.key.scancode);
break;
case SDL_EventType::SDL_EVENT_GAMEPAD_ADDED:
Input::AddGamepadDevice(event.gdevice.which);
Expand Down

0 comments on commit aa6b9f6

Please sign in to comment.