mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
Compare commits
5 Commits
aca0a9b310
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d6645e55c | |||
| a934503ec9 | |||
| 9184431b2f | |||
| f6fbcc0d89 | |||
| bc4d13c54b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,6 +8,7 @@ build/
|
|||||||
winbuild/
|
winbuild/
|
||||||
xcode/
|
xcode/
|
||||||
cmake-build-*/
|
cmake-build-*/
|
||||||
|
build-*/
|
||||||
|
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
@@ -1,20 +1,35 @@
|
|||||||
cmake_minimum_required(VERSION "3.5" FATAL_ERROR)
|
cmake_minimum_required(VERSION "3.15" FATAL_ERROR)
|
||||||
project(Lesson10 LANGUAGES C)
|
project(Lesson10 LANGUAGES C)
|
||||||
|
|
||||||
find_package(SDL3 REQUIRED CONFIG)
|
find_package(SDL3 REQUIRED CONFIG)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
|
||||||
add_executable(Lesson10 Lesson10.c)
|
set(SOURCES Lesson10.c)
|
||||||
set_target_properties(Lesson10 PROPERTIES
|
|
||||||
C_STANDARD 99
|
set(DATA
|
||||||
WIN32_EXECUTABLE ON)
|
Data/Mud.bmp
|
||||||
|
Data/World.txt)
|
||||||
|
|
||||||
|
add_executable(Lesson10 WIN32 MACOSX_BUNDLE ${SOURCES} ${DATA})
|
||||||
|
set_property(TARGET Lesson10 PROPERTY C_STANDARD 99)
|
||||||
|
source_group("Data\\Random" FILES ${DATA})
|
||||||
target_link_libraries(Lesson10 SDL3::SDL3 OpenGL::GL)
|
target_link_libraries(Lesson10 SDL3::SDL3 OpenGL::GL)
|
||||||
target_compile_options(Lesson10 PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra -pedantic>)
|
target_compile_options(Lesson10 PRIVATE $<$<C_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
|
||||||
target_compile_definitions(Lesson10 PRIVATE $<$<CXX_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
|
target_compile_definitions(Lesson10 PRIVATE $<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
|
||||||
|
|
||||||
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||||
get_property(SDL3_IMPORTED_LOCATION TARGET SDL3::SDL3 PROPERTY IMPORTED_LOCATION)
|
get_property(SDL3_IMPORTED_LOCATION TARGET SDL3::SDL3 PROPERTY IMPORTED_LOCATION)
|
||||||
if (SDL3_IMPORTED_LOCATION MATCHES "^/Library/Frameworks/")
|
if (SDL3_IMPORTED_LOCATION MATCHES "^/Library/Frameworks/")
|
||||||
set_property(TARGET Lesson10 PROPERTY BUILD_RPATH "/Library/Frameworks")
|
set_property(TARGET Lesson10 PROPERTY BUILD_RPATH "/Library/Frameworks")
|
||||||
endif()
|
endif()
|
||||||
|
foreach (RESOURCE IN LISTS DATA)
|
||||||
|
set_source_files_properties("${RESOURCE}" PROPERTIES MACOSX_PACKAGE_LOCATION "Resources/Data")
|
||||||
|
endforeach()
|
||||||
|
else()
|
||||||
|
add_custom_command(TARGET Lesson10 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
|
"${CMAKE_SOURCE_DIR}/Data" "$<TARGET_FILE_DIR:Lesson10>/Data")
|
||||||
|
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||||
|
add_custom_command(TARGET Lesson10 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
$<TARGET_FILE:SDL3::SDL3> $<TARGET_FILE_DIR:Lesson10>)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
80
Lesson10.c
80
Lesson10.c
@@ -75,6 +75,8 @@ typedef struct tagAPPSTATE
|
|||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_GLContext ctx;
|
SDL_GLContext ctx;
|
||||||
|
|
||||||
|
const char *resdir;
|
||||||
|
|
||||||
bool fullscreen, blend;
|
bool fullscreen, blend;
|
||||||
|
|
||||||
CAMERA camera;
|
CAMERA camera;
|
||||||
@@ -84,6 +86,36 @@ typedef struct tagAPPSTATE
|
|||||||
SECTOR sector1;
|
SECTOR sector1;
|
||||||
} APPSTATE;
|
} APPSTATE;
|
||||||
|
|
||||||
|
static char * resourcePath(const APPSTATE *restrict state, const char *restrict name)
|
||||||
|
{
|
||||||
|
if (!state || !name)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
size_t resdirLen = strlen(state->resdir), nameLen = strlen(name);
|
||||||
|
char *path = malloc(resdirLen + nameLen + 1);
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(path, state->resdir, resdirLen);
|
||||||
|
memcpy(&path[resdirLen], name, nameLen);
|
||||||
|
path[resdirLen + nameLen] = '\0';
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FILE * fopenResource(const APPSTATE *restrict state, const char *restrict name, const char* restrict mode)
|
||||||
|
{
|
||||||
|
char *path = NULL;
|
||||||
|
if (!mode || !(path = resourcePath(state, name)))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
FILE *f = fopen(path, mode);
|
||||||
|
free(path);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
static void readstr(FILE *f, char *string)
|
static void readstr(FILE *f, char *string)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
@@ -99,7 +131,7 @@ static void SetupWorld(APPSTATE *state)
|
|||||||
int numtriangles;
|
int numtriangles;
|
||||||
FILE *filein;
|
FILE *filein;
|
||||||
char oneline[255];
|
char oneline[255];
|
||||||
filein = fopen("data/world.txt", "r"); // File to load world data from
|
filein = fopenResource(state, "Data/World.txt", "r"); // File to load world data from
|
||||||
|
|
||||||
readstr(filein, oneline);
|
readstr(filein, oneline);
|
||||||
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
||||||
@@ -125,7 +157,7 @@ static void SetupWorld(APPSTATE *state)
|
|||||||
|
|
||||||
static bool FlipSurface(SDL_Surface *surface)
|
static bool FlipSurface(SDL_Surface *surface)
|
||||||
{
|
{
|
||||||
if (!surface || SDL_LockSurface(surface) < 0)
|
if (!surface || !SDL_LockSurface(surface))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -155,13 +187,19 @@ static bool FlipSurface(SDL_Surface *surface)
|
|||||||
|
|
||||||
static bool LoadGLTextures(APPSTATE *state)
|
static bool LoadGLTextures(APPSTATE *state)
|
||||||
{
|
{
|
||||||
SDL_Surface *TextureImage = NULL;
|
|
||||||
|
|
||||||
// Load & flip the bitmap
|
// Load & flip the bitmap
|
||||||
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
|
char *path = resourcePath(state, "Data/Mud.bmp");
|
||||||
|
if (!path)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
SDL_Surface *TextureImage = SDL_LoadBMP(path);
|
||||||
|
free(path);
|
||||||
|
if (!TextureImage || !FlipSurface(TextureImage))
|
||||||
|
{
|
||||||
|
SDL_DestroySurface(TextureImage);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
glGenTextures(3, &state->texture[0]); // Create three textures
|
glGenTextures(3, &state->texture[0]); // Create three textures
|
||||||
GLint params[3][3] =
|
GLint params[3][3] =
|
||||||
@@ -300,19 +338,19 @@ static void KillGLWindow(APPSTATE *state)
|
|||||||
// Restore windowed state & cursor visibility
|
// Restore windowed state & cursor visibility
|
||||||
if (state->fullscreen)
|
if (state->fullscreen)
|
||||||
{
|
{
|
||||||
SDL_SetWindowFullscreen(state->win, SDL_FALSE);
|
SDL_SetWindowFullscreen(state->win, false);
|
||||||
SDL_ShowCursor();
|
SDL_ShowCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release and delete rendering context
|
// Release and delete rendering context
|
||||||
if (state->ctx)
|
if (state->ctx)
|
||||||
{
|
{
|
||||||
if (SDL_GL_MakeCurrent(state->win, NULL))
|
if (!SDL_GL_MakeCurrent(state->win, NULL))
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "SHUTDOWN ERROR", "Release Of RC Failed.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "SHUTDOWN ERROR", "Release Of RC Failed.", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_DeleteContext(state->ctx);
|
SDL_GL_DestroyContext(state->ctx);
|
||||||
state->ctx = NULL;
|
state->ctx = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,7 +379,7 @@ static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height,
|
|||||||
// Try entering fullscreen mode if requested
|
// Try entering fullscreen mode if requested
|
||||||
if (fullscreenflag)
|
if (fullscreenflag)
|
||||||
{
|
{
|
||||||
if (SDL_SetWindowFullscreen(state->win, SDL_TRUE) < 0)
|
if (!SDL_SetWindowFullscreen(state->win, true))
|
||||||
{
|
{
|
||||||
// If mode switching fails, ask the user to quit or use to windowed mode
|
// If mode switching fails, ask the user to quit or use to windowed mode
|
||||||
int bttnid = ShowYesNoMessageBox(state->win, BTTN_YES, "NeHe GL",
|
int bttnid = ShowYesNoMessageBox(state->win, BTTN_YES, "NeHe GL",
|
||||||
@@ -378,7 +416,7 @@ static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SDL_GL_MakeCurrent(state->win, state->ctx)) // Activate the rendering context
|
if (!SDL_GL_MakeCurrent(state->win, state->ctx)) // Activate the rendering context
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Can't Activate The GL Rendering Context.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Can't Activate The GL Rendering Context.", NULL);
|
||||||
return false;
|
return false;
|
||||||
@@ -397,7 +435,7 @@ static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||||
{
|
{
|
||||||
APPSTATE *state = (APPSTATE *)appstate;
|
APPSTATE *state = (APPSTATE *)appstate;
|
||||||
switch (event->type)
|
switch (event->type)
|
||||||
@@ -406,15 +444,15 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
|||||||
return SDL_APP_SUCCESS;
|
return SDL_APP_SUCCESS;
|
||||||
|
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
if (event->key.keysym.sym == SDLK_ESCAPE) // Quit on escape key
|
if (event->key.key == SDLK_ESCAPE) // Quit on escape key
|
||||||
{
|
{
|
||||||
return SDL_APP_SUCCESS;
|
return SDL_APP_SUCCESS;
|
||||||
}
|
}
|
||||||
if (!event->key.repeat) // Was a key just pressed?
|
if (!event->key.repeat) // Was a key just pressed?
|
||||||
{
|
{
|
||||||
switch (event->key.keysym.sym)
|
switch (event->key.key)
|
||||||
{
|
{
|
||||||
case SDLK_b: // B = Toggle blending
|
case SDLK_B: // B = Toggle blending
|
||||||
state->blend = !state->blend;
|
state->blend = !state->blend;
|
||||||
if (!state->blend)
|
if (!state->blend)
|
||||||
{
|
{
|
||||||
@@ -428,7 +466,7 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDLK_f: // F = Cycle texture filtering
|
case SDLK_F: // F = Cycle texture filtering
|
||||||
state->filter += 1;
|
state->filter += 1;
|
||||||
if (state->filter > 2)
|
if (state->filter > 2)
|
||||||
{
|
{
|
||||||
@@ -462,14 +500,14 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
|||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppIterate(void *appstate)
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||||
{
|
{
|
||||||
APPSTATE *state = (APPSTATE *)appstate;
|
APPSTATE *state = (APPSTATE *)appstate;
|
||||||
DrawGLScene(state); // Draw the scene
|
DrawGLScene(state); // Draw the scene
|
||||||
SDL_GL_SwapWindow(state->win); // Swap buffers (double buffering)
|
SDL_GL_SwapWindow(state->win); // Swap buffers (double buffering)
|
||||||
|
|
||||||
// Handle keyboard input
|
// Handle keyboard input
|
||||||
const Uint8 *keys = SDL_GetKeyboardState(NULL);
|
const bool *keys = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_PAGEUP])
|
if (keys[SDL_SCANCODE_PAGEUP])
|
||||||
{
|
{
|
||||||
@@ -538,9 +576,9 @@ int SDL_AppIterate(void *appstate)
|
|||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppInit(void **appstate, int argc, char *argv[])
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||||
{
|
{
|
||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
@@ -555,6 +593,8 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
|
|||||||
.win = NULL,
|
.win = NULL,
|
||||||
.ctx = NULL,
|
.ctx = NULL,
|
||||||
|
|
||||||
|
.resdir = SDL_GetBasePath(),
|
||||||
|
|
||||||
.fullscreen = false,
|
.fullscreen = false,
|
||||||
.blend = false, // Blending off
|
.blend = false, // Blending off
|
||||||
|
|
||||||
@@ -589,7 +629,7 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
|
|||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL_AppQuit(void *appstate)
|
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||||
{
|
{
|
||||||
if (appstate)
|
if (appstate)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user