Skip to content
Open

Sdl3 #209

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
77 changes: 77 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
cmake_minimum_required(VERSION 3.20)

project(
davegnukem
LANGUAGES CXX
VERSION 1.0.3)

set(CMAKE_CXX_STANDARD 14)

include(GNUInstallDirs)
include(FetchContent)

# default to same version for data repo
set(DATA_TAG
${PROJECT_VERSION}
CACHE STRING "default")

FetchContent_Declare(
data
GIT_REPOSITORY https://github.com/davidjoffe/gnukem_data
GIT_TAG ${DATA_TAG})

# default to same version for datasrc repo
set(DATASRC_TAG
${PROJECT_VERSION}
CACHE STRING "default")

FetchContent_Declare(
datasrc
GIT_REPOSITORY https://github.com/davidjoffe/gnukem_datasrc
GIT_TAG ${DATASRC_TAG})

FetchContent_MakeAvailable(data datasrc)

add_subdirectory(src)

install(FILES debian/appstream/com.djoffe.davegnukem.metainfo.xml
DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
install(FILES debian/desktop/davegnukem.desktop
DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
install(DIRECTORY debian/icons DESTINATION ${CMAKE_INSTALL_DATADIR})
install(FILES HISTORY.txt README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})

install(DIRECTORY locale DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})

install(
DIRECTORY ${data_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}
PATTERN .git EXCLUDE
PATTERN .gitignore EXCLUDE
PATTERN README.md EXCLUDE PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ
WORLD_READ)
install(FILES ${data_SOURCE_DIR}/README.md
DESTINATION ${CMAKE_INSTALL_DOCDIR}-data)

install(
DIRECTORY ${datasrc_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}
PATTERN .git EXCLUDE
PATTERN .gitignore EXCLUDE
PATTERN README.md EXCLUDE
PATTERN "*.psd" EXCLUDE)
install(FILES ${datasrc_SOURCE_DIR}/README.md
DESTINATION ${CMAKE_INSTALL_DOCDIR}-datasrc)

file(READ debian/davegnukem.6 TEXT)
string(REPLACE "VERSION" "${PROJECT_VERSION}" TEXT "${TEXT}")
file(
GENERATE
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/davegnukem.6
CONTENT "${TEXT}")
set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS main.cpp)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/davegnukem.6
DESTINATION ${CMAKE_INSTALL_MANDIR}/man6)
24 changes: 24 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
file(GLOB_RECURSE HEADERS "*.h")
file(GLOB SOURCES "*.cpp")
file(GLOB SOURCES_LOC "localization/*.cpp")
file(GLOB SOURCES_SDL "sdl/*.cpp")

if(WIN32)
file(GLOB SOURCES_WIN "win32/*.cpp")
list(APPEND SOURCES ${SOURCES_WIN})
endif()

find_package(SDL3 REQUIRED)
find_package(SDL3_ttf REQUIRED)
find_package(SDL3_image REQUIRED)
find_package(SDL3_mixer REQUIRED)

add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE
${HEADERS} ${SOURCES} ${SOURCES_LOC} ${SOURCES_SDL})
target_compile_definitions(
${PROJECT_NAME}
PRIVATE DATA_DIR="${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}")
target_link_libraries(
${PROJECT_NAME} PRIVATE SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image
SDL3_mixer::SDL3_mixer)
install(TARGETS ${PROJECT_NAME})
4 changes: 2 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ extern bool g_bBigViewportMode;//dj2019-06. //Can't have both bigviewport and la
/*--------------------------------------------------------------------------*/
// Enable Unicode support [BETA] dj2022-11 (EARLY DEV - NOT ready for production do NOT yet enable in a real release - dj2022-11)
#ifndef djUNICODE_SUPPORT
//#define djUNICODE_SUPPORT
#define djUNICODE_SUPPORT
#endif
#ifdef djUNICODE_SUPPORT
#define djUNICODE_TTF
Expand All @@ -153,7 +153,7 @@ extern bool g_bBigViewportMode;//dj2019-06. //Can't have both bigviewport and la

/*--------------------------------------------------------------------------*/
// [dj2023-02] Tentative new libpng support (not yet enabled by default as it's not yet used in the actual Dave Gnukem currently, at least yet - but since we updated to SDL2 this now becomes an easier possibility to use SDL2 png loading)
//#define djUSE_SDLIMAGE
#define djUSE_SDLIMAGE
/*--------------------------------------------------------------------------*/


Expand Down
32 changes: 9 additions & 23 deletions src/djfonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
#ifdef djUNICODE_TTF
// /#include "datadir.h"//Don't actually want just for font rasterize?

// hm to move obcviously not meant to be in hiscores
#if defined(WIN32) && defined(_MSC_VER)
// Microsoft compiler stuff .. hmm not sure where best .. unless cmake etc.
#pragma comment(lib, "SDL2_ttf.lib")
#endif

/*--------------------------------------------------------------------------*/
// djFontList
/*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -170,11 +164,7 @@ TTF_Font* djUnicodeFontHelpers::FindBestMatchingFontMostCharsStr(const djFontLis
// [dj2022-11] Hmm SDL documentation says "This is the same as TTF_GlyphIsProvided(), but takes a 32-bit character instead of 16-bit, and thus can query a larger range. If you are sure you'll have an SDL_ttf that's version 2.0.18 or newer, there's no reason not to use this function exclusively."
// That means we may have a problem here supporting specifically (just) the Unicode SURROGATE PAIRS range IF (and only if) a platform has SDL older than this .. is that worth worrying about? [seems low prio to me dj2022-11 .. a few days ago we had no Unicode support at all so full Unicode support on 2.0.18+ and everything but surrogate pairs on older SDL's seems OK]
// We *definitely* want to use the 32-bit version when and where available as otherwise SURROGATE PAIRS won't work.
#if SDL_VERSION_ATLEAST(2, 0, 18)
if (c > 0 && TTF_GlyphIsProvided32(pFont, c))
#else
if (c > 0 && TTF_GlyphIsProvided(pFont, c))
#endif
if (c > 0 && TTF_FontHasGlyph(pFont, c))
{
++nMatches;
if (nMatches > nMatchesMost)
Expand Down Expand Up @@ -258,13 +248,14 @@ void djRasterizeTTFFontHelper(const std::string& sFilename, int nCharW=8, int nC
TTF_Font *font = TTF_OpenFont(sFilename.c_str(), FONT_SIZE);
if (!font)
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
printf("TTF_OpenFont: %s\n", SDL_GetError());
return;
}

// Create 32-bit with alpha channel
printf("Create 32-bit with alpha channel\n");
SDL_Surface *sprite_map = SDL_CreateRGBSurface(0, SPRITE_MAP_WIDTH, SPRITE_MAP_HEIGHT, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
auto format = SDL_GetPixelFormatForMasks(32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
SDL_Surface *sprite_map = SDL_CreateSurface(SPRITE_MAP_WIDTH, SPRITE_MAP_HEIGHT, format);
if (!sprite_map)
{
printf("SDL_CreateRGBSurface: %s\n", SDL_GetError());
Expand All @@ -287,13 +278,8 @@ void djRasterizeTTFFontHelper(const std::string& sFilename, int nCharW=8, int nC
// all the texture memory filled up with thousands of characters we maybe don't need for a language?
// We could/should also have some sort of 'fallback rendering' system(s) in future, e.g. if we were writing some sort of massively multiplayer online game with this
// then we want a system that allows people of all languages to enter text and chat and see the text.
#if SDL_VERSION_ATLEAST(2, 0, 18)
if (!TTF_GlyphIsProvided32(font, i))
if (!TTF_FontHasGlyph(font, i))
continue;
#else
if (!TTF_GlyphIsProvided(font, i))
continue;
#endif

utf8_len = 0;
utf8_buf[0] = 0;
Expand All @@ -303,10 +289,10 @@ void djRasterizeTTFFontHelper(const std::string& sFilename, int nCharW=8, int nC
if (utf8_len == 0)
continue;

SDL_Surface *character = TTF_RenderUTF8_Solid(font, utf8_buf, color);
SDL_Surface *character = TTF_RenderText_Solid(font, utf8_buf, utf8_len, color);
if (!character)
{
printf("TTF_RenderUTF8_Solid: %s\n", TTF_GetError());
printf("TTF_RenderUTF8_Solid: %s\n", SDL_GetError());
// return;
}
if (character)
Expand All @@ -333,7 +319,7 @@ void djRasterizeTTFFontHelper(const std::string& sFilename, int nCharW=8, int nC
}
//*/

SDL_FreeSurface(character);
SDL_DestroySurface(character);
}
}

Expand All @@ -343,7 +329,7 @@ void djRasterizeTTFFontHelper(const std::string& sFilename, int nCharW=8, int nC
SDL_SaveBMP(sprite_map, (sFilename + "-raster.bmp").c_str());
// Can use e.g. imagemagick + optipng on the generated .bmp to convert to .png and compress

SDL_FreeSurface(sprite_map);
SDL_DestroySurface(sprite_map);
TTF_CloseFont(font);
}
/*
Expand Down
6 changes: 1 addition & 5 deletions src/djfonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

// forward? keep definition for .cpp backend?
//struct TTF_Font;
#ifdef __OS2__
#include <SDL/SDL_ttf.h>
#else
#include <SDL_ttf.h>
#endif
#include <SDL3_ttf/SDL_ttf.h>



Expand Down
6 changes: 3 additions & 3 deletions src/djgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Copyright (C) 1998-2024 David Joffe
// [todo tidy this all up and simplify and maybe add a small simple light wrapper helper .h (or more than one) to help with this stuff]
// Want to try be slightly smarter with __has_include (but it's C++17 only) so don't want to uncomment these things yet until I've tested it better so leaving it commented for now
// All our includes are done like "SDL.h" and I've seen samples online that do it but it seems wrong because "" means check current directory first, which is davegnukem which obviously does not have SDL.h so shouldn't these all be <SDL.h> etc.? I think if we're on C++7 or higher it's a good idea to maybe try detect and do fallbacks etc.
#include "SDL.h"
#include <SDL3/SDL.h>
//#endif

#endif
Expand Down Expand Up @@ -72,7 +72,7 @@ class djVisual
width = 0;
height = 0;
stride = 0;
bpp = 0;
format = SDL_PIXELFORMAT_ABGR8888;
pixwidth = 0;
m_bFullscreen = false;
}
Expand All @@ -85,7 +85,7 @@ class djVisual
int width;
int height;
int stride;
int bpp; // 32 ... 24? (pixsize) pixdepth=24, pixsize=32 width=4
SDL_PixelFormat format; // 32 ... 24? (pixsize) pixdepth=24, pixsize=32 width=4
int pixwidth; // 4
bool m_bFullscreen;
};
Expand Down
15 changes: 6 additions & 9 deletions src/djimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ dj2022-11 Note that since we're now on SDL2 we could potentially use e.g. SDLima
#include "djgraph.h"
#include "sys_error.h"
#include <string.h>
#ifdef __OS2__
#include <SDL/SDL_endian.h>
#else
#include <SDL_endian.h>
#endif
#include <SDL3/SDL_endian.h>

/*--------------------------------------------------------------------------*/
// TGA types
enum EfdTGAType
Expand Down Expand Up @@ -252,12 +249,12 @@ int djImage::Load( const char * szFilename )
if (szFilename == NULL) return -1; // NULL string
if (szFilename[0] == 0) return -1; // empty string

std::string filename = szFilename;
std::string extension = filename.substr(filename.find_last_of(".") + 1);
std::string filename = szFilename;
std::string extension = filename.substr(filename.find_last_of(".") + 1);
extern void djStrToLowerTmpHelper( std::string& s );
djStrToLowerTmpHelper(extension);
djStrToLowerTmpHelper(extension);

// For TGA files pass to our own old TGA loader
// For TGA files pass to our own old TGA loader
if (extension == "tga")
{
// fixme why are we bothering with all this? we only load TGA
Expand Down
34 changes: 16 additions & 18 deletions src/djimageload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
Copyright (C) 1998-2024 David Joffe
*/
/*--------------------------------------------------------------------------*/
#include "djimageload.h"
#include "config.h"
#include "djimage.h"
#include "djimageload.h"
#include <SDL3/SDL_endian.h>
#include <string>
#ifdef __OS2__
#include <SDL/SDL_endian.h>
#else
#include <SDL_endian.h>
#endif

#ifdef djUSE_SDLIMAGE
// Hm not 100% sure if we should best put here 'SDL2/SDL_image.h' or just 'SDL_image.h' and let makefiles etc. pass the folder in ..
#include <SDL_image.h>
#include <SDL3_image/SDL_image.h>
#endif

//-----------------------------------------
Expand Down Expand Up @@ -213,21 +210,22 @@ int djImageLoad::LoadImage(djImage* pImg, const char *szFilename)
return -1;
}
// if small endian am i supposed to reverse the masks here? not sure ..
printf("CreateImage:%d %d %d pitch=%d rgba %08x %08x %08x %08x\n", (int)surface->w, (int)surface->h, (int)surface->format->BitsPerPixel, (int)surface->pitch,
(int)surface->format->Rmask,
(int)surface->format->Gmask,
(int)surface->format->Bmask,
(int)surface->format->Amask);
pImg->CreateImage(surface->w, surface->h, surface->format->BitsPerPixel, surface->pitch, surface->pixels,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask
auto f = SDL_GetPixelFormatDetails(surface->format);
printf("CreateImage:%d %d %d pitch=%d rgba %08x %08x %08x %08x\n", (int)surface->w, (int)surface->h, (int)f->bits_per_pixel, (int)surface->pitch,
(int)f->Rmask,
(int)f->Gmask,
(int)f->Bmask,
(int)f->Amask);
pImg->CreateImage(surface->w, surface->h, f->bits_per_pixel, surface->pitch, surface->pixels,
f->Rmask,
f->Gmask,
f->Bmask,
f->Amask
);
printf("CreateImage:DONE\n");

// Clean up
SDL_FreeSurface(surface);
SDL_DestroySurface(surface);
return 0;
#else
return -1;//unhandled format
Expand Down
6 changes: 1 addition & 5 deletions src/djinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
#ifndef _DJINPUT_H_
#define _DJINPUT_H_
/*--------------------------------------------------------------------------*/
#ifdef __OS2__
#include <SDL/SDL_events.h>
#else
#include <SDL_events.h>
#endif
#include <SDL3/SDL_events.h>

#define DJKEY_MAX 256
extern int g_iKeys[DJKEY_MAX];
Expand Down
Loading