correctness pass

This commit is contained in:
2024-03-12 20:45:35 +11:00
parent 9ea9665e70
commit bfa90f838d

View File

@@ -14,15 +14,15 @@
#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> // Header File For The OpenGL32 Library
SDL_Window* hWnd = NULL; // Holds Our Window Handle SDL_Window* win = NULL; // Holds Our Window Handle
SDL_GLContext hRC = NULL; // Permanent Rendering Context SDL_GLContext ctx = NULL; // Permanent Rendering Context
bool keys[SDL_NUM_SCANCODES]; // Array Used For The Keyboard Routine bool keys[SDL_NUM_SCANCODES]; // Array Used For The Keyboard Routine
bool active = true; // Window Active Flag Set To TRUE By Default bool active = true; // Window Active Flag Set To TRUE By Default
bool fullscreen = true; // Fullscreen Flag Set To Fullscreen Mode By Default bool fullscreen = true; // Fullscreen Flag Set To Fullscreen Mode By Default
bool blend; // Blending ON/OFF bool blend = false; // Blending ON/OFF
bool bp; // B Pressed? bool bpressed = false; // B Pressed?
bool fp; // F Pressed? bool fpressed = false; // F Pressed?
static const SDL_MessageBoxButtonData yesnobttns[2] = static const SDL_MessageBoxButtonData yesnobttns[2] =
{ {
@@ -40,17 +40,17 @@ static const SDL_MessageBoxButtonData yesnobttns[2] =
const float piover180 = 0.0174532925f; const float piover180 = 0.0174532925f;
float heading; float heading;
float xpos; float xpos, zpos;
float zpos;
GLfloat yrot; // Y Rotation GLfloat yrot; // Y Rotation
GLfloat walkbias = 0; GLfloat walkbias = 0.0f;
GLfloat walkbiasangle = 0; GLfloat walkbiasangle = 0.0f;
GLfloat lookupdown = 0.0f; GLfloat lookupdown = 0.0f;
GLfloat z = 0.0f; // Depth Into The Screen GLfloat z = 0.0f; // Depth Into The Screen
GLuint filter; // Which Filter To Use GLuint filter; // Which Filter To Use
GLuint texture[3]; // Storage For 3 Textures // Storage For 3 Textures
GLuint texture[3] = { 0, 0, 0};
typedef struct tagVERTEX typedef struct tagVERTEX
{ {
@@ -69,7 +69,8 @@ typedef struct tagSECTOR
TRIANGLE* triangle; TRIANGLE* triangle;
} SECTOR; } SECTOR;
SECTOR sector1; // Our Model Goes Here: // Our Model Goes Here:
SECTOR sector1 = { .numtriangles = 0, .triangle = NULL };
void readstr(FILE *f, char *string) void readstr(FILE *f, char *string)
{ {
@@ -80,13 +81,13 @@ void readstr(FILE *f, char *string)
return; return;
} }
void SetupWorld() void SetupWorld(void)
{ {
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", "rt"); // File To Load World Data From filein = fopen("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);
@@ -140,7 +141,7 @@ bool FlipSurface(SDL_Surface *surface)
return true; return true;
} }
int LoadGLTextures() // Load Bitmaps And Convert To Textures int LoadGLTextures(void) // Load Bitmaps And Convert To Textures
{ {
int Status = SDL_FALSE; // Status Indicator int Status = SDL_FALSE; // Status Indicator
@@ -177,6 +178,12 @@ int LoadGLTextures() // Load Bitmaps And Convert To Textures
return Status; // Return The Status return Status; // Return The Status
} }
void FreeResources(void)
{
free(sector1.triangle);
glDeleteTextures(3, texture);
}
static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{ {
double h = 1.0 / tan(fovy * (SDL_PI_D / 180.0) * 0.5); double h = 1.0 / tan(fovy * (SDL_PI_D / 180.0) * 0.5);
@@ -194,7 +201,7 @@ static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdou
glLoadMatrixd(mtx); glLoadMatrixd(mtx);
} }
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window void ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{ {
if (height == 0) // Prevent A Divide By Zero By if (height == 0) // Prevent A Divide By Zero By
{ {
@@ -213,7 +220,7 @@ GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initializ
glLoadIdentity(); // Reset The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix
} }
int InitGL() // All Setup For OpenGL Goes Here int InitGL(void) // All Setup For OpenGL Goes Here
{ {
if (!LoadGLTextures()) // Jump To Texture Loading Routine if (!LoadGLTextures()) // Jump To Texture Loading Routine
{ {
@@ -234,7 +241,7 @@ int InitGL() // All Setup For OpenGL
return 1; // Initialization Went OK return 1; // Initialization Went OK
} }
int DrawGLScene() // Here's Where We Do All The Drawing int DrawGLScene(void) // Here's Where We Do All The Drawing
{ {
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 Screen And The Depth Buffer
glLoadIdentity(); // Reset The View glLoadIdentity(); // Reset The View
@@ -285,27 +292,27 @@ int DrawGLScene() // Here's Where We Do A
return 1; // Everything Went OK return 1; // Everything Went OK
} }
GLvoid KillGLWindow() // Properly Kill The Window void KillGLWindow(void) // Properly Kill The Window
{ {
if (fullscreen) // Are We In Fullscreen Mode? if (fullscreen) // Are We In Fullscreen Mode?
{ {
SDL_SetWindowFullscreen(hWnd, 0); // If So Switch Back To The Desktop SDL_SetWindowFullscreen(win, 0); // If So Switch Back To The Desktop
SDL_ShowCursor(); // Show Mouse Pointer SDL_ShowCursor(); // Show Mouse Pointer
} }
if (hRC) // Do We Have A Rendering Context? if (ctx) // Do We Have A Rendering Context?
{ {
if (SDL_GL_MakeCurrent(hWnd, NULL)) // Are We Able To Release The DC And RC Contexts? if (SDL_GL_MakeCurrent(win, NULL)) // Are We Able To Release The DC And RC Contexts?
{ {
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(hRC); // Delete The RC SDL_GL_DeleteContext(ctx); // Delete The RC
hRC = NULL; // Set RC To NULL ctx = NULL; // Set RC To NULL
} }
SDL_DestroyWindow(hWnd); // Destroy The Window SDL_DestroyWindow(win); // Destroy The Window
hWnd = NULL; // Set hWnd To NULL win = NULL; // Set hWnd To NULL
} }
@@ -317,7 +324,7 @@ GLvoid KillGLWindow() // Properly Kill The Window
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
bool CreateGLWindow(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 SDL_Rect WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.x = 0; // Set Left Value To 0 WindowRect.x = 0; // Set Left Value To 0
@@ -328,7 +335,7 @@ bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
fullscreen = fullscreenflag; // Set The Global Fullscreen Flag fullscreen = fullscreenflag; // Set The Global Fullscreen Flag
// Create The Window // Create The Window
if (!(hWnd = SDL_CreateWindow(title, if (!(win = SDL_CreateWindow(title,
WindowRect.w, // Window Width WindowRect.w, // Window Width
WindowRect.h, // Window Height WindowRect.h, // Window Height
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY))) SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY)))
@@ -340,13 +347,13 @@ bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
if (fullscreen) // Attempt Fullscreen Mode? if (fullscreen) // Attempt Fullscreen Mode?
{ {
if (SDL_SetWindowFullscreen(hWnd, SDL_TRUE) < 0) // Try To Set Selected Mode And Get Results. if (SDL_SetWindowFullscreen(win, SDL_TRUE) < 0) // Try To Set Selected Mode And Get Results.
{ {
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
const SDL_MessageBoxData msgbox = const SDL_MessageBoxData msgbox =
{ {
SDL_MESSAGEBOX_INFORMATION, SDL_MESSAGEBOX_INFORMATION,
hWnd, win,
"NeHe GL", "NeHe GL",
"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",
2, 2,
@@ -385,26 +392,27 @@ bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); // No Stencil Buffer SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); // No Stencil Buffer
if (!(hRC = SDL_GL_CreateContext(hWnd))) // Are We Able To Get A Rendering Context? if (!(ctx = SDL_GL_CreateContext(win))) // Are We Able To Get A Rendering Context?
{ {
KillGLWindow(); // Reset The Display KillGLWindow(); // Reset The Display
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; // Return FALSE
} }
if (SDL_GL_MakeCurrent(hWnd, hRC)) // Try To Activate The Rendering Context if (SDL_GL_MakeCurrent(win, ctx)) // Try To Activate The Rendering Context
{ {
KillGLWindow(); // Reset The Display KillGLWindow(); // Reset The Display
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; // Return FALSE
} }
SDL_ShowWindow(hWnd); SDL_ShowWindow(win);
SDL_GL_SetSwapInterval(1); SDL_GL_SetSwapInterval(1);
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
if (!InitGL()) // Initialize Our Newly Created GL Window if (!InitGL()) // Initialize Our Newly Created GL Window
{ {
FreeResources();
KillGLWindow(); // Reset The Display KillGLWindow(); // Reset The Display
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 FALSE
@@ -413,7 +421,7 @@ bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
return true; // Success return true; // Success
} }
void WndProc(SDL_Event* uMsg) void WndProc(SDL_Event *uMsg)
{ {
switch (uMsg->type) // Check For Windows Messages switch (uMsg->type) // Check For Windows Messages
{ {
@@ -437,7 +445,7 @@ void WndProc(SDL_Event* uMsg)
} }
} }
int main(int argc, char* argv[]) int main(int argc, char *argv[])
{ {
SDL_Event msg; // Windows Message Structure SDL_Event msg; // Windows Message Structure
bool done = false; // Bool Variable To Exit Loop bool done = false; // Bool Variable To Exit Loop
@@ -448,7 +456,7 @@ int main(int argc, char* argv[])
const SDL_MessageBoxData msgbox = const SDL_MessageBoxData msgbox =
{ {
/* flags */ SDL_MESSAGEBOX_INFORMATION, /* flags */ SDL_MESSAGEBOX_INFORMATION,
/* window */ hWnd, /* window */ win,
/* title */ "Start FullScreen?", /* title */ "Start FullScreen?",
/* message */ "Would You Like To Run In Fullscreen Mode?", /* message */ "Would You Like To Run In Fullscreen Mode?",
/* numbuttons */ 2, /* numbuttons */ 2,
@@ -490,10 +498,10 @@ int main(int argc, char* argv[])
} }
else // Not Time To Quit, Update Screen else // Not Time To Quit, Update Screen
{ {
SDL_GL_SwapWindow(hWnd); // Swap Buffers (Double Buffering) SDL_GL_SwapWindow(win); // Swap Buffers (Double Buffering)
if (keys[SDL_SCANCODE_B] && !bp) if (keys[SDL_SCANCODE_B] && !bpressed)
{ {
bp = true; bpressed = true;
blend = !blend; blend = !blend;
if (!blend) if (!blend)
{ {
@@ -508,12 +516,12 @@ int main(int argc, char* argv[])
} }
if (!keys[SDL_SCANCODE_B]) if (!keys[SDL_SCANCODE_B])
{ {
bp = false; bpressed = false;
} }
if (keys[SDL_SCANCODE_F] && !fp) if (keys[SDL_SCANCODE_F] && !fpressed)
{ {
fp = true; fpressed = true;
filter += 1; filter += 1;
if (filter > 2) if (filter > 2)
{ {
@@ -522,7 +530,7 @@ int main(int argc, char* argv[])
} }
if (!keys[SDL_SCANCODE_F]) if (!keys[SDL_SCANCODE_F])
{ {
fp = false; fpressed = false;
} }
if (keys[SDL_SCANCODE_PAGEUP]) if (keys[SDL_SCANCODE_PAGEUP])
@@ -591,6 +599,7 @@ int main(int argc, char* argv[])
if (keys[SDL_SCANCODE_F1]) // Is F1 Being Pressed? if (keys[SDL_SCANCODE_F1]) // Is F1 Being Pressed?
{ {
keys[SDL_SCANCODE_F1] = false; // If So Make Key FALSE keys[SDL_SCANCODE_F1] = false; // If So Make Key FALSE
FreeResources();
KillGLWindow(); // Kill Our Current Window KillGLWindow(); // Kill Our Current Window
fullscreen = !fullscreen; // Toggle Fullscreen / Windowed Mode fullscreen = !fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window // Recreate Our OpenGL Window
@@ -604,6 +613,7 @@ int main(int argc, char* argv[])
} }
// Shutdown // Shutdown
FreeResources();
KillGLWindow(); // Kill The Window KillGLWindow(); // Kill The Window
SDL_Quit(); SDL_Quit();
return EXIT_SUCCESS; // Exit The Program return EXIT_SUCCESS; // Exit The Program