mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
Compare commits
3 Commits
f6fbcc0d89
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d6645e55c | |||
| a934503ec9 | |||
| 9184431b2f |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,6 +8,7 @@ build/
|
||||
winbuild/
|
||||
xcode/
|
||||
cmake-build-*/
|
||||
build-*/
|
||||
|
||||
Thumbs.db
|
||||
.DS_Store
|
||||
|
||||
@@ -4,10 +4,15 @@ project(Lesson10 LANGUAGES C)
|
||||
find_package(SDL3 REQUIRED CONFIG)
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
add_executable(Lesson10 Lesson10.c)
|
||||
set_target_properties(Lesson10 PROPERTIES
|
||||
C_STANDARD 99
|
||||
WIN32_EXECUTABLE ON)
|
||||
set(SOURCES Lesson10.c)
|
||||
|
||||
set(DATA
|
||||
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_compile_options(Lesson10 PRIVATE $<$<C_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
|
||||
target_compile_definitions(Lesson10 PRIVATE $<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
|
||||
@@ -17,4 +22,14 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||
if (SDL3_IMPORTED_LOCATION MATCHES "^/Library/Frameworks/")
|
||||
set_property(TARGET Lesson10 PROPERTY BUILD_RPATH "/Library/Frameworks")
|
||||
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()
|
||||
|
||||
60
Lesson10.c
60
Lesson10.c
@@ -75,6 +75,8 @@ typedef struct tagAPPSTATE
|
||||
SDL_Window *win;
|
||||
SDL_GLContext ctx;
|
||||
|
||||
const char *resdir;
|
||||
|
||||
bool fullscreen, blend;
|
||||
|
||||
CAMERA camera;
|
||||
@@ -84,6 +86,36 @@ typedef struct tagAPPSTATE
|
||||
SECTOR sector1;
|
||||
} 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)
|
||||
{
|
||||
do
|
||||
@@ -99,7 +131,7 @@ static void SetupWorld(APPSTATE *state)
|
||||
int numtriangles;
|
||||
FILE *filein;
|
||||
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);
|
||||
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
||||
@@ -155,13 +187,19 @@ static bool FlipSurface(SDL_Surface *surface)
|
||||
|
||||
static bool LoadGLTextures(APPSTATE *state)
|
||||
{
|
||||
SDL_Surface *TextureImage = NULL;
|
||||
|
||||
// Load & flip the bitmap
|
||||
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
|
||||
char *path = resourcePath(state, "Data/Mud.bmp");
|
||||
if (!path)
|
||||
{
|
||||
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
|
||||
GLint params[3][3] =
|
||||
@@ -300,14 +338,14 @@ static void KillGLWindow(APPSTATE *state)
|
||||
// Restore windowed state & cursor visibility
|
||||
if (state->fullscreen)
|
||||
{
|
||||
SDL_SetWindowFullscreen(state->win, SDL_FALSE);
|
||||
SDL_SetWindowFullscreen(state->win, false);
|
||||
SDL_ShowCursor();
|
||||
}
|
||||
|
||||
// Release and delete rendering context
|
||||
if (state->ctx)
|
||||
{
|
||||
if (SDL_GL_MakeCurrent(state->win, NULL) == SDL_FALSE)
|
||||
if (!SDL_GL_MakeCurrent(state->win, NULL))
|
||||
{
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "SHUTDOWN ERROR", "Release Of RC Failed.", NULL);
|
||||
}
|
||||
@@ -341,7 +379,7 @@ static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height,
|
||||
// Try entering fullscreen mode if requested
|
||||
if (fullscreenflag)
|
||||
{
|
||||
if (!SDL_SetWindowFullscreen(state->win, SDL_TRUE))
|
||||
if (!SDL_SetWindowFullscreen(state->win, true))
|
||||
{
|
||||
// If mode switching fails, ask the user to quit or use to windowed mode
|
||||
int bttnid = ShowYesNoMessageBox(state->win, BTTN_YES, "NeHe GL",
|
||||
@@ -469,7 +507,7 @@ SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
SDL_GL_SwapWindow(state->win); // Swap buffers (double buffering)
|
||||
|
||||
// Handle keyboard input
|
||||
const SDL_bool *keys = SDL_GetKeyboardState(NULL);
|
||||
const bool *keys = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (keys[SDL_SCANCODE_PAGEUP])
|
||||
{
|
||||
@@ -540,7 +578,7 @@ SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == SDL_FALSE)
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
@@ -555,6 +593,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
.win = NULL,
|
||||
.ctx = NULL,
|
||||
|
||||
.resdir = SDL_GetBasePath(),
|
||||
|
||||
.fullscreen = false,
|
||||
.blend = false, // Blending off
|
||||
|
||||
@@ -589,7 +629,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate)
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||
{
|
||||
if (appstate)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user