mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
Compare commits
11 Commits
56a21bfa68
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d6645e55c | |||
| a934503ec9 | |||
| 9184431b2f | |||
| f6fbcc0d89 | |||
| bc4d13c54b | |||
| aca0a9b310 | |||
| 62062b4f7b | |||
| ce23f15169 | |||
| 43d531fa4c | |||
| 4fa31fce4e | |||
| 1ef509f90f |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -5,8 +5,12 @@ CMakeSettings.json
|
|||||||
|
|
||||||
out/
|
out/
|
||||||
build/
|
build/
|
||||||
|
winbuild/
|
||||||
xcode/
|
xcode/
|
||||||
cmake-build-*/
|
cmake-build-*/
|
||||||
|
build-*/
|
||||||
|
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
*.exe
|
||||||
|
*.dll
|
||||||
|
|||||||
@@ -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/")
|
||||||
target_link_options(Lesson10 PRIVATE -Wl,-rpath,/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()
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
553
Lesson10.c
553
Lesson10.c
@@ -1,54 +1,58 @@
|
|||||||
/*
|
/*
|
||||||
* This Code Was Created By Lionel Brits & Jeff Molofee 2000
|
* This code was created by Lionel Brits & Jeff Molofee 2000.
|
||||||
* A HUGE Thanks To Fredric Echols For Cleaning Up
|
* A HUGE thanks to Fredric Echols for cleaning up
|
||||||
* And Optimizing The Base Code, Making It More Flexible!
|
* and optimizing the base code, making it more flexible!
|
||||||
* If You've Found This Code Useful, Please Let Me Know.
|
* If you've found this code useful, please let me know.
|
||||||
* Visit My Site At nehe.gamedev.net
|
* Visit my site at https://nehe.gamedev.net
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <math.h> // Math Library Header File
|
#include <math.h>
|
||||||
#include <stdio.h> // Header File For Standard Input/Output
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#define SDL_MAIN_USE_CALLBACKS
|
#define SDL_MAIN_USE_CALLBACKS
|
||||||
#include <SDL3/SDL_main.h>
|
#include <SDL3/SDL_main.h>
|
||||||
#include <SDL3/SDL_opengl.h> // Header File For The OpenGL32 Library
|
#include <SDL3/SDL_opengl.h>
|
||||||
|
|
||||||
SDL_Window* win = NULL; // Holds Our Window Handle
|
#define BTTN_YES 0
|
||||||
SDL_GLContext ctx = NULL; // Permanent Rendering Context
|
#define BTTN_NO 1
|
||||||
|
|
||||||
bool fullscreen = false;
|
static int ShowYesNoMessageBox(SDL_Window *window, int defaultbttn, const char *title, const char *message)
|
||||||
bool blend = false; // Blending ON/OFF
|
|
||||||
|
|
||||||
static const SDL_MessageBoxButtonData yesnobttns[2] =
|
|
||||||
{
|
{
|
||||||
|
const SDL_MessageBoxButtonFlags defaultflags =
|
||||||
|
SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT | SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
|
||||||
|
const SDL_MessageBoxButtonData yesnobttns[2] =
|
||||||
{
|
{
|
||||||
/*flags */ 0,
|
{ .flags = defaultbttn == BTTN_YES ? defaultflags : 0, .buttonID = BTTN_YES, .text = "Yes" },
|
||||||
/*buttonid */ 0,
|
{ .flags = defaultbttn == BTTN_NO ? defaultflags : 0, .buttonID = BTTN_NO, .text = "No" }
|
||||||
/*text */ "Yes"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
/*flags */ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT | SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
|
|
||||||
/*buttonid */ 1,
|
|
||||||
/*text */ "No"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
const SDL_MessageBoxData msgbox =
|
||||||
|
{
|
||||||
|
.flags = SDL_MESSAGEBOX_INFORMATION,
|
||||||
|
.window = window,
|
||||||
|
.title = title,
|
||||||
|
.message = message,
|
||||||
|
.numbuttons = 2,
|
||||||
|
.buttons = yesnobttns,
|
||||||
|
.colorScheme = NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
int bttnid = defaultbttn;
|
||||||
|
SDL_ShowMessageBox(&msgbox, &bttnid);
|
||||||
|
return bttnid;
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct tagCAMERA
|
typedef struct tagCAMERA
|
||||||
{
|
{
|
||||||
float heading;
|
float heading;
|
||||||
float xpos, zpos;
|
float xpos, zpos;
|
||||||
float yrot; // Y Rotation
|
float yrot;
|
||||||
float walkbias, walkbiasangle;
|
float walkbias, walkbiasangle;
|
||||||
float lookupdown;
|
float lookupdown;
|
||||||
float z; // Depth Into The Screen
|
float z;
|
||||||
} CAMERA;
|
} CAMERA;
|
||||||
|
|
||||||
CAMERA camera;
|
|
||||||
unsigned filter; // Which Filter To Use
|
|
||||||
GLuint texture[3] = { 0, 0, 0 }; // Storage For 3 Textures
|
|
||||||
|
|
||||||
typedef struct tagVERTEX
|
typedef struct tagVERTEX
|
||||||
{
|
{
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
@@ -66,10 +70,53 @@ typedef struct tagSECTOR
|
|||||||
TRIANGLE *triangle;
|
TRIANGLE *triangle;
|
||||||
} SECTOR;
|
} SECTOR;
|
||||||
|
|
||||||
// Our Model Goes Here:
|
typedef struct tagAPPSTATE
|
||||||
SECTOR sector1 = { .numtriangles = 0, .triangle = NULL };
|
{
|
||||||
|
SDL_Window *win;
|
||||||
|
SDL_GLContext ctx;
|
||||||
|
|
||||||
void readstr(FILE *f, char *string)
|
const char *resdir;
|
||||||
|
|
||||||
|
bool fullscreen, blend;
|
||||||
|
|
||||||
|
CAMERA camera;
|
||||||
|
unsigned filter; // Filtered texture selection
|
||||||
|
GLuint texture[3]; // Filtered textures
|
||||||
|
|
||||||
|
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
|
do
|
||||||
{
|
{
|
||||||
@@ -78,39 +125,39 @@ void readstr(FILE *f, char *string)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetupWorld(void)
|
static void SetupWorld(APPSTATE *state)
|
||||||
{
|
{
|
||||||
float x, y, z, u, v;
|
float x, y, z, u, v;
|
||||||
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);
|
||||||
|
|
||||||
sector1.triangle = malloc(sizeof(TRIANGLE) * numtriangles);
|
state->sector1.triangle = malloc(sizeof(TRIANGLE) * numtriangles);
|
||||||
sector1.numtriangles = numtriangles;
|
state->sector1.numtriangles = numtriangles;
|
||||||
for (int loop = 0; loop < numtriangles; loop++)
|
for (int loop = 0; loop < numtriangles; loop++)
|
||||||
{
|
{
|
||||||
for (int vert = 0; vert < 3; vert++)
|
for (int vert = 0; vert < 3; vert++)
|
||||||
{
|
{
|
||||||
readstr(filein, oneline);
|
readstr(filein, oneline);
|
||||||
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
|
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
|
||||||
sector1.triangle[loop].vertex[vert].x = x;
|
state->sector1.triangle[loop].vertex[vert].x = x;
|
||||||
sector1.triangle[loop].vertex[vert].y = y;
|
state->sector1.triangle[loop].vertex[vert].y = y;
|
||||||
sector1.triangle[loop].vertex[vert].z = z;
|
state->sector1.triangle[loop].vertex[vert].z = z;
|
||||||
sector1.triangle[loop].vertex[vert].u = u;
|
state->sector1.triangle[loop].vertex[vert].u = u;
|
||||||
sector1.triangle[loop].vertex[vert].v = v;
|
state->sector1.triangle[loop].vertex[vert].v = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(filein);
|
fclose(filein);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -138,17 +185,23 @@ bool FlipSurface(SDL_Surface *surface)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LoadGLTextures(void) // Load bitmap as textures
|
static bool LoadGLTextures(APPSTATE *state)
|
||||||
{
|
{
|
||||||
SDL_Surface *TextureImage = NULL; // Create Storage Space For The Texture
|
// Load & flip the bitmap
|
||||||
|
char *path = resourcePath(state, "Data/Mud.bmp");
|
||||||
// Load & flip the bitmap, check for errors
|
if (!path)
|
||||||
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
SDL_Surface *TextureImage = SDL_LoadBMP(path);
|
||||||
|
free(path);
|
||||||
|
if (!TextureImage || !FlipSurface(TextureImage))
|
||||||
|
{
|
||||||
|
SDL_DestroySurface(TextureImage);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
glGenTextures(3, &texture[0]); // Create three textures
|
glGenTextures(3, &state->texture[0]); // Create three textures
|
||||||
GLint params[3][3] =
|
GLint params[3][3] =
|
||||||
{
|
{
|
||||||
[0] = { GL_NEAREST, GL_NEAREST, GL_FALSE }, // Nearest filtered
|
[0] = { GL_NEAREST, GL_NEAREST, GL_FALSE }, // Nearest filtered
|
||||||
@@ -157,7 +210,7 @@ bool LoadGLTextures(void) // Load bitmap as textures
|
|||||||
};
|
};
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[i]);
|
glBindTexture(GL_TEXTURE_2D, state->texture[i]);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, params[i][0]);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, params[i][0]);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, params[i][1]);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, params[i][1]);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, params[i][2]);
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, params[i][2]);
|
||||||
@@ -166,7 +219,8 @@ bool LoadGLTextures(void) // Load bitmap as textures
|
|||||||
0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage->pixels);
|
0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage->pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_DestroySurface(TextureImage); // Free the image structure
|
// Free temporary surface
|
||||||
|
SDL_DestroySurface(TextureImage);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,235 +241,220 @@ static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdou
|
|||||||
glLoadMatrixd(mtx);
|
glLoadMatrixd(mtx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReSizeGLScene(int width, int height) // Resize And Initialize The GL Window
|
/* Set viewport size and setup matrices *
|
||||||
|
* width - Width of the OpenGL framebuffer *
|
||||||
|
* height - Height of the OpenGL framebuffer */
|
||||||
|
static void ReSizeGLScene(int width, int height)
|
||||||
{
|
{
|
||||||
if (height == 0) // Prevent A Divide By Zero By
|
if (height == 0) // Prevent division-by-zero by ensuring height is non-zero
|
||||||
{
|
{
|
||||||
height = 1; // Making Height Equal One
|
height = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glViewport(0, 0, width, height); // Reset The Current Viewport
|
glViewport(0, 0, width, height); // Reset the current viewport
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
|
glMatrixMode(GL_PROJECTION); // Select & reset the projection matrix
|
||||||
glLoadIdentity(); // Reset The Projection Matrix
|
glLoadIdentity();
|
||||||
|
|
||||||
// Calculate The Aspect Ratio Of The Window
|
float aspect = (float)width / (float)height; // Calculate aspect ratio
|
||||||
gluPerspective(45.0f, (float)width / (float)height, 0.1f, 100.0f);
|
gluPerspective(45.0f, aspect, 0.1f, 100.0f); // Setup perspective matrix
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
|
glMatrixMode(GL_MODELVIEW); // Select & reset the modelview matrix
|
||||||
glLoadIdentity(); // Reset The Modelview Matrix
|
glLoadIdentity();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InitGL(void) // All Setup For OpenGL Goes Here
|
static bool InitGL(APPSTATE *state)
|
||||||
{
|
{
|
||||||
if (!LoadGLTextures()) // Jump To Texture Loading Routine
|
if (!LoadGLTextures(state)) // Load textures
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
|
glEnable(GL_TEXTURE_2D); // Enable texture mapping
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set The Blending Function For Translucency
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set the blending function for translucency
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set the background clear color to black
|
||||||
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
|
glClearDepth(1.0); // Ensure depth buffer clears to furthest value
|
||||||
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
|
glDepthFunc(GL_LESS); // Pass if pixel depth value tests less than the depth buffer value
|
||||||
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
glEnable(GL_DEPTH_TEST); // Enable depth testing
|
||||||
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
|
glShadeModel(GL_SMOOTH); // Enable Smooth color shading
|
||||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Request the implementation to use perspective correct interpolation
|
||||||
|
|
||||||
SetupWorld();
|
SetupWorld(state);
|
||||||
|
|
||||||
return true; // Initialization Went OK
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawGLScene(void) // Here's Where We Do All The Drawing
|
static void DrawGLScene(APPSTATE *state)
|
||||||
{
|
{
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the color and depth buffers
|
||||||
glLoadIdentity(); // Reset The View
|
glLoadIdentity(); // Reset modelview
|
||||||
|
|
||||||
GLfloat x_m, y_m, z_m, u_m, v_m;
|
GLfloat x_m, y_m, z_m, u_m, v_m;
|
||||||
GLfloat xtrans = -camera.xpos;
|
GLfloat xtrans = -state->camera.xpos;
|
||||||
GLfloat ztrans = -camera.zpos;
|
GLfloat ztrans = -state->camera.zpos;
|
||||||
GLfloat ytrans = -camera.walkbias - 0.25f;
|
GLfloat ytrans = -state->camera.walkbias - 0.25f;
|
||||||
GLfloat sceneroty = 360.0f - camera.yrot;
|
GLfloat sceneroty = 360.0f - state->camera.yrot;
|
||||||
|
|
||||||
int numtriangles;
|
int numtriangles;
|
||||||
|
|
||||||
glRotatef(camera.lookupdown, 1.0f, 0.0f, 0.0f);
|
glRotatef(state->camera.lookupdown, 1.0f, 0.0f, 0.0f);
|
||||||
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
|
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
glTranslatef(xtrans, ytrans, ztrans);
|
glTranslatef(xtrans, ytrans, ztrans);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[filter]);
|
glBindTexture(GL_TEXTURE_2D, state->texture[state->filter]);
|
||||||
|
|
||||||
numtriangles = sector1.numtriangles;
|
numtriangles = state->sector1.numtriangles;
|
||||||
|
|
||||||
// Process Each Triangle
|
|
||||||
for (int loop_m = 0; loop_m < numtriangles; loop_m++)
|
for (int loop_m = 0; loop_m < numtriangles; loop_m++)
|
||||||
{
|
{
|
||||||
glBegin(GL_TRIANGLES);
|
glBegin(GL_TRIANGLES);
|
||||||
glNormal3f(0.0f, 0.0f, 1.0f);
|
glNormal3f(0.0f, 0.0f, 1.0f);
|
||||||
x_m = sector1.triangle[loop_m].vertex[0].x;
|
x_m = state->sector1.triangle[loop_m].vertex[0].x;
|
||||||
y_m = sector1.triangle[loop_m].vertex[0].y;
|
y_m = state->sector1.triangle[loop_m].vertex[0].y;
|
||||||
z_m = sector1.triangle[loop_m].vertex[0].z;
|
z_m = state->sector1.triangle[loop_m].vertex[0].z;
|
||||||
u_m = sector1.triangle[loop_m].vertex[0].u;
|
u_m = state->sector1.triangle[loop_m].vertex[0].u;
|
||||||
v_m = sector1.triangle[loop_m].vertex[0].v;
|
v_m = state->sector1.triangle[loop_m].vertex[0].v;
|
||||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||||
|
|
||||||
x_m = sector1.triangle[loop_m].vertex[1].x;
|
x_m = state->sector1.triangle[loop_m].vertex[1].x;
|
||||||
y_m = sector1.triangle[loop_m].vertex[1].y;
|
y_m = state->sector1.triangle[loop_m].vertex[1].y;
|
||||||
z_m = sector1.triangle[loop_m].vertex[1].z;
|
z_m = state->sector1.triangle[loop_m].vertex[1].z;
|
||||||
u_m = sector1.triangle[loop_m].vertex[1].u;
|
u_m = state->sector1.triangle[loop_m].vertex[1].u;
|
||||||
v_m = sector1.triangle[loop_m].vertex[1].v;
|
v_m = state->sector1.triangle[loop_m].vertex[1].v;
|
||||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||||
|
|
||||||
x_m = sector1.triangle[loop_m].vertex[2].x;
|
x_m = state->sector1.triangle[loop_m].vertex[2].x;
|
||||||
y_m = sector1.triangle[loop_m].vertex[2].y;
|
y_m = state->sector1.triangle[loop_m].vertex[2].y;
|
||||||
z_m = sector1.triangle[loop_m].vertex[2].z;
|
z_m = state->sector1.triangle[loop_m].vertex[2].z;
|
||||||
u_m = sector1.triangle[loop_m].vertex[2].u;
|
u_m = state->sector1.triangle[loop_m].vertex[2].u;
|
||||||
v_m = sector1.triangle[loop_m].vertex[2].v;
|
v_m = state->sector1.triangle[loop_m].vertex[2].v;
|
||||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KillGLWindow(void) // Properly Kill The Window
|
static void KillGLWindow(APPSTATE *state)
|
||||||
{
|
{
|
||||||
if (fullscreen) // Are We In Fullscreen Mode?
|
// Restore windowed state & cursor visibility
|
||||||
|
if (state->fullscreen)
|
||||||
{
|
{
|
||||||
SDL_SetWindowFullscreen(win, SDL_FALSE); // If So Switch Back To The Desktop
|
SDL_SetWindowFullscreen(state->win, false);
|
||||||
SDL_ShowCursor(); // Show Mouse Pointer
|
SDL_ShowCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx) // Do We Have A Rendering Context?
|
// Release and delete rendering context
|
||||||
|
if (state->ctx)
|
||||||
{
|
{
|
||||||
if (SDL_GL_MakeCurrent(win, NULL)) // Are We Able To Release The DC And RC Contexts?
|
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(ctx); // Delete The RC
|
SDL_GL_DestroyContext(state->ctx);
|
||||||
ctx = NULL; // Set RC To NULL
|
state->ctx = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_DestroyWindow(win); // Destroy The Window
|
SDL_DestroyWindow(state->win);
|
||||||
win = NULL; // Set hWnd To NULL
|
state->win = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* This Code Creates Our OpenGL Window. Parameters Are: *
|
/* This code creates our OpenGL window, parameters are: *
|
||||||
* title - Title To Appear At The Top Of The Window *
|
* title - Title to appear at the top of the window *
|
||||||
* width - Width Of The GL Window Or Fullscreen Mode *
|
* width - Width of the OpenGL window or fullscreen mode *
|
||||||
* height - Height Of The GL Window Or Fullscreen Mode *
|
* height - Height of the OpenGL window or fullscreen mode *
|
||||||
* bits - Number Of Bits To Use For Color (8/16/24/32) *
|
* bits - Number of bits to use for color (8/16/24/32) *
|
||||||
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
|
* fullscreenflag - Use fullscreen mode (true) Or windowed mode (false) */
|
||||||
|
static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height, int bits, bool fullscreenflag)
|
||||||
|
|
||||||
bool CreateGLWindow(char *title, int width, int height, int bits, bool fullscreenflag)
|
|
||||||
{
|
{
|
||||||
SDL_Rect WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
|
state->fullscreen = fullscreenflag;
|
||||||
WindowRect.x = 0; // Set Left Value To 0
|
|
||||||
WindowRect.w = width; // Set Right Value To Requested Width
|
|
||||||
WindowRect.y = 0; // Set Top Value To 0
|
|
||||||
WindowRect.h = height; // Set Bottom Value To Requested Height
|
|
||||||
|
|
||||||
fullscreen = fullscreenflag; // Set The Global Fullscreen Flag
|
if (!(state->win = SDL_CreateWindow(title, width, height,
|
||||||
|
|
||||||
// Create The Window
|
|
||||||
if (!(win = SDL_CreateWindow(title,
|
|
||||||
WindowRect.w, // Window Width
|
|
||||||
WindowRect.h, // Window Height
|
|
||||||
SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY)))
|
SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY)))
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Window Creation Error.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Window Creation Error.", NULL);
|
||||||
return false; // Return FALSE
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fullscreen) // Attempt Fullscreen Mode?
|
// Try entering fullscreen mode if requested
|
||||||
|
if (fullscreenflag)
|
||||||
{
|
{
|
||||||
if (SDL_SetWindowFullscreen(win, SDL_TRUE) < 0) // Try To Set Selected Mode And Get Results.
|
if (!SDL_SetWindowFullscreen(state->win, true))
|
||||||
{
|
{
|
||||||
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
|
// If mode switching fails, ask the user to quit or use to windowed mode
|
||||||
const SDL_MessageBoxData msgbox =
|
int bttnid = ShowYesNoMessageBox(state->win, BTTN_YES, "NeHe GL",
|
||||||
|
"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?");
|
||||||
|
if (bttnid == BTTN_NO)
|
||||||
{
|
{
|
||||||
SDL_MESSAGEBOX_INFORMATION,
|
// User chose to quit
|
||||||
win,
|
|
||||||
"NeHe GL",
|
|
||||||
"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",
|
|
||||||
2,
|
|
||||||
yesnobttns,
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
int bttnid = 0;
|
|
||||||
SDL_ShowMessageBox(&msgbox, &bttnid);
|
|
||||||
if (bttnid == 1)
|
|
||||||
{
|
|
||||||
// Pop Up A Message Box Letting User Know The Program Is Closing.
|
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "ERROR", "Program Will Now Close.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "ERROR", "Program Will Now Close.", NULL);
|
||||||
return false; // Return FALSE
|
return false;
|
||||||
}
|
}
|
||||||
fullscreen = false;
|
state->fullscreen = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Must Support Double Buffering
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Double buffered framebuffer
|
||||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 0); // Color Bits Ignored
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 0); // Color bits ignored
|
||||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 0);
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 0);
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // No Alpha Buffer
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // No alpha buffer
|
||||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); // No Accumulation Buffer
|
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); // No accumulation buffer
|
||||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); // Accumulation Bits Ignored
|
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); // Accumulation bits ignored
|
||||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
|
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
|
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // 16Bit Z-Buffer (Depth Buffer)
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // 16-bit Z-buffer (depth buffer)
|
||||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); // No Stencil Buffer
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); // No stencil buffer
|
||||||
|
|
||||||
|
|
||||||
if (!(ctx = SDL_GL_CreateContext(win))) // Are We Able To Get A Rendering Context?
|
if (!(state->ctx = SDL_GL_CreateContext(state->win))) // Create rendering context
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Can't Create A GL Rendering Context.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Can't Create A GL Rendering Context.", NULL);
|
||||||
return false; // Return FALSE
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SDL_GL_MakeCurrent(win, ctx)) // Try To 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
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_ShowWindow(win);
|
SDL_ShowWindow(state->win);
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1); // Enable VSync
|
||||||
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
|
ReSizeGLScene(width, height); // Set up our viewport and perspective
|
||||||
|
|
||||||
if (!InitGL()) // Initialize Our Newly Created GL Window
|
if (!InitGL(state)) // Initialize the scene
|
||||||
{
|
{
|
||||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Initialization Failed.", NULL);
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR", "Initialization Failed.", NULL);
|
||||||
return false; // Return FALSE
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true; // Success
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppEvent(const SDL_Event *event)
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||||
{
|
{
|
||||||
|
APPSTATE *state = (APPSTATE *)appstate;
|
||||||
switch (event->type)
|
switch (event->type)
|
||||||
{
|
{
|
||||||
case SDL_EVENT_QUIT: // Have we received a quit event?
|
case SDL_EVENT_QUIT: // Have we received a quit event?
|
||||||
return 1; // Exit with success status
|
return SDL_APP_SUCCESS;
|
||||||
|
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
if (event->key.keysym.sym == SDLK_ESCAPE) // Quit on Escape
|
if (event->key.key == SDLK_ESCAPE) // Quit on escape key
|
||||||
{
|
{
|
||||||
return 1; // Exit with success status
|
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
|
||||||
blend = !blend;
|
state->blend = !state->blend;
|
||||||
if (!blend)
|
if (!state->blend)
|
||||||
{
|
{
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
@@ -425,142 +464,141 @@ int SDL_AppEvent(const SDL_Event *event)
|
|||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
}
|
}
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
case SDLK_f: // F = Cycle texture filtering
|
case SDLK_F: // F = Cycle texture filtering
|
||||||
filter += 1;
|
state->filter += 1;
|
||||||
if (filter > 2)
|
if (state->filter > 2)
|
||||||
{
|
{
|
||||||
filter = 0;
|
state->filter = 0;
|
||||||
}
|
}
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
case SDLK_F1: // F1 = Toggle Fullscreen / Windowed Mode
|
case SDLK_F1: // F1 = Toggle fullscreen / windowed mode
|
||||||
SDL_SetWindowFullscreen(win, !fullscreen);
|
SDL_SetWindowFullscreen(state->win, !state->fullscreen);
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
default: return 0;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: // Deal with window resizes
|
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: // Deal with window resizes
|
||||||
ReSizeGLScene(event->window.data1, event->window.data2); // data1=Backbuffer Width, data2=Backbuffer Height
|
ReSizeGLScene(event->window.data1, event->window.data2); // data1=Backbuffer Width, data2=Backbuffer Height
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
|
case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
|
||||||
fullscreen = true;
|
state->fullscreen = true;
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
|
case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
|
||||||
fullscreen = false;
|
state->fullscreen = false;
|
||||||
return 0;
|
break;
|
||||||
|
|
||||||
default: return 0;
|
default: break;
|
||||||
}
|
}
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppIterate(void)
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||||
{
|
{
|
||||||
DrawGLScene(); // Draw the scene
|
APPSTATE *state = (APPSTATE *)appstate;
|
||||||
SDL_GL_SwapWindow(win); // Swap buffers (Double buffering)
|
DrawGLScene(state); // Draw the scene
|
||||||
|
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])
|
||||||
{
|
{
|
||||||
camera.z -= 0.02f;
|
state->camera.z -= 0.02f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_PAGEDOWN])
|
if (keys[SDL_SCANCODE_PAGEDOWN])
|
||||||
{
|
{
|
||||||
camera.z += 0.02f;
|
state->camera.z += 0.02f;
|
||||||
}
|
}
|
||||||
|
|
||||||
const float piover180 = 0.0174532925f;
|
const float piover180 = 0.0174532925f;
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_UP])
|
if (keys[SDL_SCANCODE_UP])
|
||||||
{
|
{
|
||||||
camera.xpos -= sinf(camera.heading * piover180) * 0.05f;
|
state->camera.xpos -= sinf(state->camera.heading * piover180) * 0.05f;
|
||||||
camera.zpos -= cosf(camera.heading * piover180) * 0.05f;
|
state->camera.zpos -= cosf(state->camera.heading * piover180) * 0.05f;
|
||||||
if (camera.walkbiasangle >= 359.0f)
|
if (state->camera.walkbiasangle >= 359.0f)
|
||||||
{
|
{
|
||||||
camera.walkbiasangle = 0.0f;
|
state->camera.walkbiasangle = 0.0f;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
camera.walkbiasangle += 10;
|
state->camera.walkbiasangle += 10;
|
||||||
}
|
}
|
||||||
camera.walkbias = sinf(camera.walkbiasangle * piover180) / 20.0f;
|
state->camera.walkbias = sinf(state->camera.walkbiasangle * piover180) / 20.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_DOWN])
|
if (keys[SDL_SCANCODE_DOWN])
|
||||||
{
|
{
|
||||||
camera.xpos += sinf(camera.heading * piover180) * 0.05f;
|
state->camera.xpos += sinf(state->camera.heading * piover180) * 0.05f;
|
||||||
camera.zpos += cosf(camera.heading * piover180) * 0.05f;
|
state->camera.zpos += cosf(state->camera.heading * piover180) * 0.05f;
|
||||||
if (camera.walkbiasangle <= 1.0f)
|
if (state->camera.walkbiasangle <= 1.0f)
|
||||||
{
|
{
|
||||||
camera.walkbiasangle = 359.0f;
|
state->camera.walkbiasangle = 359.0f;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
camera.walkbiasangle -= 10;
|
state->camera.walkbiasangle -= 10;
|
||||||
}
|
}
|
||||||
camera.walkbias = sinf(camera.walkbiasangle * piover180) / 20.0f;
|
state->camera.walkbias = sinf(state->camera.walkbiasangle * piover180) / 20.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_RIGHT])
|
if (keys[SDL_SCANCODE_RIGHT])
|
||||||
{
|
{
|
||||||
camera.heading -= 1.0f;
|
state->camera.heading -= 1.0f;
|
||||||
camera.yrot = camera.heading;
|
state->camera.yrot = state->camera.heading;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_LEFT])
|
if (keys[SDL_SCANCODE_LEFT])
|
||||||
{
|
{
|
||||||
camera.heading += 1.0f;
|
state->camera.heading += 1.0f;
|
||||||
camera.yrot = camera.heading;
|
state->camera.yrot = state->camera.heading;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_PAGEUP])
|
if (keys[SDL_SCANCODE_PAGEUP])
|
||||||
{
|
{
|
||||||
camera.lookupdown -= 1.0f;
|
state->camera.lookupdown -= 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_PAGEDOWN])
|
if (keys[SDL_SCANCODE_PAGEDOWN])
|
||||||
{
|
{
|
||||||
camera.lookupdown += 1.0f;
|
state->camera.lookupdown += 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SDL_AppInit(int argc, char *argv[])
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||||
|
|
||||||
// Ask The User Which Screen Mode They Prefer
|
|
||||||
const SDL_MessageBoxData msgbox =
|
|
||||||
{
|
{
|
||||||
/* flags */ SDL_MESSAGEBOX_INFORMATION,
|
return SDL_APP_FAILURE;
|
||||||
/* window */ win,
|
|
||||||
/* title */ "Start FullScreen?",
|
|
||||||
/* message */ "Would You Like To Run In Fullscreen Mode?",
|
|
||||||
/* numbuttons */ 2,
|
|
||||||
/* buttons */ yesnobttns,
|
|
||||||
/* colorScheme */ NULL
|
|
||||||
};
|
|
||||||
int bttnid = 1;
|
|
||||||
SDL_ShowMessageBox(&msgbox, &bttnid);
|
|
||||||
|
|
||||||
// Create Our OpenGL Window
|
|
||||||
const bool wantfullscreen = (bttnid == 0);
|
|
||||||
if (!CreateGLWindow("Lionel Brits & NeHe's 3D World Tutorial", 640, 480, 16, wantfullscreen))
|
|
||||||
{
|
|
||||||
return -1; // Quit If Window Was Not Created
|
|
||||||
}
|
}
|
||||||
|
|
||||||
camera = (CAMERA)
|
APPSTATE *state = *appstate = malloc(sizeof(APPSTATE));
|
||||||
|
if (!state)
|
||||||
|
{
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
*state = (APPSTATE)
|
||||||
|
{
|
||||||
|
.win = NULL,
|
||||||
|
.ctx = NULL,
|
||||||
|
|
||||||
|
.resdir = SDL_GetBasePath(),
|
||||||
|
|
||||||
|
.fullscreen = false,
|
||||||
|
.blend = false, // Blending off
|
||||||
|
|
||||||
|
.camera = (CAMERA)
|
||||||
{
|
{
|
||||||
.heading = 0.0f,
|
.heading = 0.0f,
|
||||||
.xpos = 0.0f,
|
.xpos = 0.0f,
|
||||||
@@ -570,19 +608,40 @@ int SDL_AppInit(int argc, char *argv[])
|
|||||||
.walkbiasangle = 0.0f,
|
.walkbiasangle = 0.0f,
|
||||||
.lookupdown = 0.0f,
|
.lookupdown = 0.0f,
|
||||||
.z = 0.0f
|
.z = 0.0f
|
||||||
|
},
|
||||||
|
|
||||||
|
.filter = 0,
|
||||||
|
.texture = { 0, 0, 0 },
|
||||||
|
.sector1 = (SECTOR){ .numtriangles = 0, .triangle = NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
return 0;
|
// Ask the user if they would like to start in fullscreen or windowed mode
|
||||||
|
int bttnid = ShowYesNoMessageBox(state->win, BTTN_NO, "Start FullScreen?",
|
||||||
|
"Would You Like To Run In Fullscreen Mode?");
|
||||||
|
|
||||||
|
// Create our OpenGL window
|
||||||
|
const bool wantfullscreen = (bttnid == BTTN_YES);
|
||||||
|
if (!CreateGLWindow(state, "Lionel Brits & NeHe's 3D World Tutorial", 640, 480, 16, wantfullscreen))
|
||||||
|
{
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL_AppQuit()
|
return SDL_APP_CONTINUE;
|
||||||
{
|
|
||||||
// Shutdown
|
|
||||||
free(sector1.triangle);
|
|
||||||
if (ctx)
|
|
||||||
{
|
|
||||||
glDeleteTextures(3, texture);
|
|
||||||
}
|
}
|
||||||
KillGLWindow(); // Kill The Window
|
|
||||||
|
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||||
|
{
|
||||||
|
if (appstate)
|
||||||
|
{
|
||||||
|
APPSTATE *state = (APPSTATE *)appstate;
|
||||||
|
free(state->sector1.triangle);
|
||||||
|
if (state->ctx)
|
||||||
|
{
|
||||||
|
glDeleteTextures(3, state->texture);
|
||||||
|
}
|
||||||
|
KillGLWindow(state);
|
||||||
|
free(state);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user