Avancées sdl_image et inclusion de SDL2TTF pour dprint()

This commit is contained in:
attilavs2 2023-11-27 10:49:47 +01:00
parent 36176c1b42
commit 8f3fc85cf5
11 changed files with 341 additions and 68 deletions

View File

@ -21,6 +21,7 @@ find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)
find_package(SDL2_image REQUIRED)
find_package(SDL2TTF REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_IMAGE_INCLUDE_DIRS})

Binary file not shown.

163
cmake/FindSDL2TTF.cmake Normal file
View File

@ -0,0 +1,163 @@
# Locate SDL2 library
# This module defines
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).
#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
SET(SDL2TTF_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_PATH(SDL2TTF_INCLUDE_DIR SDL_ttf.h
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2TTF_SEARCH_PATHS}
)
FIND_LIBRARY(SDL2TTF_LIBRARY_TEMP
NAMES SDL2_ttf
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2TTF_SEARCH_PATHS}
)
IF(NOT SDL2TTF_BUILDING_LIBRARY)
IF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2TTFmain. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2TTFmain for compatibility even though they don't
# necessarily need it.
FIND_LIBRARY(SDL2TTFMAIN_LIBRARY
NAMES SDL2_ttf
HINTS
$ENV{SDL2TTFDIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2TTF_SEARCH_PATHS}
)
ENDIF(NOT ${SDL2TTF_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2TTF_BUILDING_LIBRARY)
# SDL2TTF may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)
# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDL2TTFmain -lSDL2TTF -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2TTF_LIBRARY_TEMP)
# For SDL2TTFmain
IF(NOT SDL2TTF_BUILDING_LIBRARY)
IF(SDL2TTFMAIN_LIBRARY)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTFMAIN_LIBRARY} ${SDL2TTF_LIBRARY_TEMP})
ENDIF(SDL2TTFMAIN_LIBRARY)
ENDIF(NOT SDL2TTF_BUILDING_LIBRARY)
# For OS X, SDL2TTF uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} "-framework Cocoa")
ENDIF(APPLE)
# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2TTF_LIBRARY_TEMP ${SDL2TTF_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)
# For MinGW library
IF(MINGW)
SET(SDL2TTF_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2TTF_LIBRARY_TEMP})
ENDIF(MINGW)
# Set the final string here so the GUI reflects the final state.
SET(SDL2TTF_LIBRARY ${SDL2TTF_LIBRARY_TEMP} CACHE STRING "Where the SDL2TTF Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2TTF_LIBRARY_TEMP "${SDL2TTF_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2TTF_LIBRARY_TEMP)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2TTF REQUIRED_VARS SDL2TTF_LIBRARY SDL2TTF_INCLUDE_DIR)

View File

@ -0,0 +1,53 @@
# sdl2_ttf cmake project-config input for CMakeLists.txt script
include(FeatureSummary)
set_package_properties(SDL2_ttf PROPERTIES
URL "https://www.libsdl.org/projects/SDL_ttf/"
DESCRIPTION "Support for TrueType (.ttf) font files with Simple Directmedia Layer"
)
set(SDL2_ttf_FOUND ON)
set(SDL2TTF_VENDORED @SDL2TTF_VENDORED@)
set(SDL2TTF_HARFBUZZ @SDL2TTF_HARFBUZZ@)
set(SDL2TTF_FREETYPE @SDL2TTF_FREETYPE@)
set(SDL2TTF_SDL2_REQUIRED_VERSION @SDL_REQUIRED_VERSION@)
include(CMakeFindDependencyMacro)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL2_ttf-shared-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL2_ttf-shared-targets.cmake")
endif()
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL2_ttf-static-targets.cmake")
if(SDL2TTF_VENDORED)
include(CheckLanguage)
check_language(CXX)
if(NOT CMAKE_CXX_COMPILER AND NOT _sdl2ttf_nowarning)
message(WARNING "CXX language not enabled. Linking to SDL2_ttf::SDL2_ttf-static might fail.")
endif()
endif()
include("${CMAKE_CURRENT_LIST_DIR}/SDL2_ttf-static-targets.cmake")
endif()
if(NOT SDL2TTF_VENDORED)
set(_sdl_cmake_module_path "${CMAKE_MODULE_PATH}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
if(TARGET SDL2_ttf::SDL2_ttf-static)
if(SDL2TTF_FREETYPE)
find_dependency(Freetype)
endif()
if(SDL2TTF_HARFBUZZ)
list(APPEND harfbuzz_ROOT "${CMAKE_CURRENT_LIST_DIR}")
find_dependency(harfbuzz)
endif()
endif()
set(CMAKE_MODULE_PATH "${_sdl_cmake_module_path}")
unset(_sdl_cmake_module_path)
endif()

View File

@ -45,4 +45,4 @@
#define tindex_size 256
#define tsize 64
#endif /* map__h */
#endif

View File

@ -33,6 +33,7 @@
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "sdl_image.h"

View File

@ -20,11 +20,15 @@ uint8_t rc_keydown(enum keys key) {
case 0: {return 0;}
default:{return keydown(keymap[key]);}
}
return 255;
}
//To reimplement
uint8_t rc_getkey(enum keys key) {
dupdate();
while (!keydown(keymap[key])){pollevent();}
return 255;
}
#endif
@ -58,9 +62,9 @@ void rc_pollevent(){
}
uint8_t rc_keydown(enum keys key){
return 255;
}
//To reimplement
uint8_t rc_getkey(enum keys key) {
dupdate();

View File

@ -64,7 +64,7 @@ int main(){
//trucs de chargement
//image_t *vram = image_create_vram();
image_t *vram;// = image_create_vram();
image_t *sky_tex = image_alloc(64, 64, IMAGE_RGB565);
image_t *D_tex = image_alloc(64, 64, IMAGE_RGB565);
@ -96,6 +96,8 @@ int main(){
player.plane.y = fix(-0.66F);
player.speed_mult = 0xFFFF;
while (exit_game == 0) {
prof_t frame = prof_make();
prof_enter(frame);
@ -108,29 +110,28 @@ int main(){
//if(first_frame == 1) main_menu();
keys_get(&game);
//keys_get(&game);
//logic();
if (disp_frame_time == 1) dprint( 1, 1, C_BLACK, "Frame time : %d ms", frame_time);
/*if (disp_frame_time == 1) dprint( 1, 1, C_BLACK, "Frame time : %d ms", frame_time);
#ifdef debug
dprint( 1, 10,C_BLACK, "Raycast time : %d ms", raycast_time);
dprint( 1, 20,C_BLACK, "Draw time : %d ms", draw_time);
dprint( 1, 30, C_BLACK, "planeX : %d", player1.plane.x);
dprint( 1, 40, C_BLACK, "planeY : %d", player1.plane.y);
dprint( 1, 50, C_BLACK, "dirX : %d", player1.dir.x);
dprint( 1, 60, C_BLACK, "dirY : %d", player1.dir.y);
dprint( 1, 70, C_BLACK, "posX : %d", player1.pos.x);
dprint( 1, 80, C_BLACK, "posY : %d", player1.pos.y);
dprint( 1, 90, C_BLACK, "posZ : %d", player1.pos.z);
#ifdef FXCG50
dprint( 1, 130, C_BLACK, "RAM usage : %d",
kmalloc_get_gint_stats(kmalloc_get_arena("_uram"))->used_memory);
#endif
#endif
dprint( 1, 10,C_BLACK, "Raycast time : %d ms", raycast_time);
dprint( 1, 20,C_BLACK, "Draw time : %d ms", draw_time);
dprint( 1, 30, C_BLACK, "planeX : %d", player1.plane.x);
dprint( 1, 40, C_BLACK, "planeY : %d", player1.plane.y);
dprint( 1, 50, C_BLACK, "dirX : %d", player1.dir.x);
dprint( 1, 60, C_BLACK, "dirY : %d", player1.dir.y);
dprint( 1, 70, C_BLACK, "posX : %d", player1.pos.x);
dprint( 1, 80, C_BLACK, "posY : %d", player1.pos.y);
dprint( 1, 90, C_BLACK, "posZ : %d", player1.pos.z);
#ifdef FXCG50
dprint( 1, 130, C_BLACK, "RAM usage : %d",
kmalloc_get_gint_stats(kmalloc_get_arena("_uram"))->used_memory);
#endif
#endif*/
dupdate();

View File

@ -151,7 +151,7 @@ void logic(){
void draw_f(image_t *vram){ //a refaire
}
/*
void draw_walls(Game *game, image_t *vram){
Player *player = game->player;
ShooterMap *shooterLevel = game->shooterLevel;
@ -322,13 +322,13 @@ void draw_walls(Game *game, image_t *vram){
texSampleY = 0;
}
//image_sub(tex_index[tex], texX, texSampleY, 1, texSample, tex_stripe);
image_sub(tex_index[tex], texX, texSampleY, 1, texSample, tex_stripe);
//image_scale(tex_stripe, 0xFFFF, texSize, &temp);
image_scale(tex_stripe, 0xFFFF, texSize, &temp);
//image_linear(tex_stripe, image_at(vram, x, linePos), &temp); //Plante ici
image_linear(tex_stripe, image_at(vram, x, linePos), &temp); //Plante ici
gint_dvline(linePos, linePos+lineHeight, x, C_PINK);
//gint_dvline(linePos, linePos+lineHeight, x, C_PINK);
prof_leave(img_drw);
prof_enter(rayscat);
@ -351,4 +351,4 @@ void draw_walls(Game *game, image_t *vram){
}
raycast_time = (int)prof_time(rayscat)/1000;
}*/
}

View File

@ -109,10 +109,52 @@ void dupdate(void){
SDL_RenderPresent(SDL_inf.renderer);
}
static SDL_Rect dSubrect;
void dimage(int x, int y, image_t *image){
extern SDL_Info SDL_inf;
SDL_RenderCopy(SDL_inf.renderer, image, image->subrect, image->subrect);
dSubrect.x = x;
dSubrect.y = y;
dSubrect.w = image->subrect->w;
dSubrect.h = image->subrect->h;
SDL_RenderCopy(SDL_inf.renderer, image->tex, image->subrect, &dSubrect);
}
static image_t image_sub_default;
image_t *image_sub(image_t *src, int x, int y, int w, int h, image_t *dest){
if(!dest) dest = &image_sub_default;
if(w < 0) w = src->subrect->w - x;
if(h < 0) h = src->subrect->h - y;
dest->subrect->x = x;
dest->subrect->y = y;
dest->subrect->w = w;
dest->subrect->h = h;
dest->tex = src->tex;
return dest;
}
void image_scale(image_t const *src, int gamma_x, int gamma_y, struct image_linear_map *map){
map->h_scale = f2float(gamma_x);
map->v_scale = f2float(gamma_y);
}
void image_linear(image_t const *src, image_t *dst, struct image_linear_map *map){
extern SDL_Info SDL_inf;
dSubrect.x = 0;
dSubrect.y = 0;
dSubrect.w = round(src->subrect->w * map->h_scale);
dSubrect.h = round(src->subrect->h * map->v_scale);
SDL_RenderCopy(SDL_inf.renderer, src->tex, src->subrect, &dSubrect);
}
#endif

View File

@ -10,7 +10,7 @@
// - Importation (sdl_tex_load() ) - 20%
// - gint/display.h :
// - dprint() - 5%
// - dimage() - 0%
// - dimage() - 80%
// - dclear() - 100%
// - dupdate() - 100%
// - gint_dvline() - 0%
@ -21,9 +21,9 @@
// - image_alloc() - 90% - no leaks but validity wasn't tested
// - image_free() - 90%
// - image_fill() - 0%
// - image_sub() - 0%
// - image_scale() - 0%
// - image_linear() - 0%
// - image_sub() - 90%
// - image_scale() - 40%
// - image_linear() - 50%
// - libprof :
// - prof_t - 90%
// - prof_make - X
@ -42,12 +42,7 @@
#define C_BSKY (SDL_Color){156, 182, 239, 255}
#define C_DGRAY (SDL_Color){123, 121, 123, 255}
// Taken from gint/image.h
enum img_type{
IMAGE_RGB565 = 0, /* Converted to RGBA8888 but with alpha = 255*/
IMAGE_RGB565A = 1, /* RGBA8888 but with alpha = 0 */
};
//From gint/image.h
/* image_t: gint's native bitmap image format
Images of this format can be created through this header's API but also by
@ -60,37 +55,14 @@ typedef struct {
} image_t;
typedef struct{
uint32_t start;
uint32_t current;
uint8_t rec;
} prof_t;
int prof_init(void);
void prof_quit(void);
#define prof_make() ((prof_t){0,0,0})
#define prof_enter(prof) { if(1>prof.rec){ prof.start=SDL_GetTicks(); prof.rec=1; }}
#define prof_leave(prof) { if(prof.rec>1){ prof.current=SDL_GetTicks(); prof.rec=0; }}
#define prof_time(prof) (prof.current-prof.start)
void dprint(int x, int y, SDL_Color fg, char const *format, ...);
void dclear(SDL_Color color);
void dupdate(void);
void gint_dvline(int x1, int x2, int x, SDL_Color color);
void dimage(int x, int y, image_t *image);
enum img_type{
IMAGE_RGB565 = 0, /* Converted to RGBA8888*/
IMAGE_RGB565A = 1
};
struct image_linear_map{
fixed_t h_scale;
fixed_t v_scale;
float h_scale;
float v_scale;
};
image_t *image_create_vram(void);
@ -110,6 +82,42 @@ void image_free(image_t *img);
void image_scale(image_t const *src, int gamma_x, int gamma_y, struct image_linear_map *map);
void image_linear(image_t const *src, image_t *dst, struct image_linear_map *map);
//From gint/display.h
void dprint(int x, int y, SDL_Color fg, char const *format, ...);
void dclear(SDL_Color color);
void dupdate(void);
void dimage(int x, int y, image_t *image);
//From gint/render.h
void gint_dvline(int x1, int x2, int x, SDL_Color color);
//Libprof
typedef struct{
uint32_t start;
uint32_t current;
uint8_t rec;
} prof_t;
int prof_init(void);
void prof_quit(void);
#define prof_make() ((prof_t){0,0,0})
#define prof_enter(prof) { if(1>prof.rec){ prof.start=SDL_GetTicks(); prof.rec=1; }}
#define prof_leave(prof) { if(prof.rec>1){ prof.current=SDL_GetTicks(); prof.rec=0; }}
#define prof_time(prof) (prof.current-prof.start)
//SDL specific defs
typedef struct{