mirror of
https://github.com/ScrelliCopter/Lesson10-SDL3.git
synced 2025-02-21 07:19:26 +11:00
Compare commits
6 Commits
56a21bfa68
...
aca0a9b310
| Author | SHA1 | Date | |
|---|---|---|---|
| aca0a9b310 | |||
| 62062b4f7b | |||
| ce23f15169 | |||
| 43d531fa4c | |||
| 4fa31fce4e | |||
| 1ef509f90f |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -5,8 +5,11 @@ CMakeSettings.json
|
||||
|
||||
out/
|
||||
build/
|
||||
winbuild/
|
||||
xcode/
|
||||
cmake-build-*/
|
||||
|
||||
Thumbs.db
|
||||
.DS_Store
|
||||
*.exe
|
||||
*.dll
|
||||
|
||||
@@ -15,6 +15,6 @@ target_compile_definitions(Lesson10 PRIVATE $<$<CXX_COMPILER_ID:MSVC>:_CRT_SECUR
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||
get_property(SDL3_IMPORTED_LOCATION TARGET SDL3::SDL3 PROPERTY IMPORTED_LOCATION)
|
||||
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()
|
||||
endif()
|
||||
|
||||
515
Lesson10.c
515
Lesson10.c
@@ -1,54 +1,58 @@
|
||||
/*
|
||||
* This Code Was Created By Lionel Brits & Jeff Molofee 2000
|
||||
* A HUGE Thanks To Fredric Echols For Cleaning Up
|
||||
* And Optimizing The Base Code, Making It More Flexible!
|
||||
* If You've Found This Code Useful, Please Let Me Know.
|
||||
* Visit My Site At nehe.gamedev.net
|
||||
* This code was created by Lionel Brits & Jeff Molofee 2000.
|
||||
* A HUGE thanks to Fredric Echols for cleaning up
|
||||
* and optimizing the base code, making it more flexible!
|
||||
* If you've found this code useful, please let me know.
|
||||
* Visit my site at https://nehe.gamedev.net
|
||||
*/
|
||||
|
||||
#include <math.h> // Math Library Header File
|
||||
#include <stdio.h> // Header File For Standard Input/Output
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#define SDL_MAIN_USE_CALLBACKS
|
||||
#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
|
||||
SDL_GLContext ctx = NULL; // Permanent Rendering Context
|
||||
#define BTTN_YES 0
|
||||
#define BTTN_NO 1
|
||||
|
||||
bool fullscreen = false;
|
||||
bool blend = false; // Blending ON/OFF
|
||||
|
||||
static const SDL_MessageBoxButtonData yesnobttns[2] =
|
||||
static int ShowYesNoMessageBox(SDL_Window *window, int defaultbttn, const char *title, const char *message)
|
||||
{
|
||||
const SDL_MessageBoxButtonFlags defaultflags =
|
||||
SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT | SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
|
||||
const SDL_MessageBoxButtonData yesnobttns[2] =
|
||||
{
|
||||
/*flags */ 0,
|
||||
/*buttonid */ 0,
|
||||
/*text */ "Yes"
|
||||
},
|
||||
{ .flags = defaultbttn == BTTN_YES ? defaultflags : 0, .buttonID = BTTN_YES, .text = "Yes" },
|
||||
{ .flags = defaultbttn == BTTN_NO ? defaultflags : 0, .buttonID = BTTN_NO, .text = "No" }
|
||||
};
|
||||
const SDL_MessageBoxData msgbox =
|
||||
{
|
||||
/*flags */ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT | SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
|
||||
/*buttonid */ 1,
|
||||
/*text */ "No"
|
||||
}
|
||||
};
|
||||
.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
|
||||
{
|
||||
float heading;
|
||||
float xpos, zpos;
|
||||
float yrot; // Y Rotation
|
||||
float yrot;
|
||||
float walkbias, walkbiasangle;
|
||||
float lookupdown;
|
||||
float z; // Depth Into The Screen
|
||||
float z;
|
||||
} CAMERA;
|
||||
|
||||
CAMERA camera;
|
||||
unsigned filter; // Which Filter To Use
|
||||
GLuint texture[3] = { 0, 0, 0 }; // Storage For 3 Textures
|
||||
|
||||
typedef struct tagVERTEX
|
||||
{
|
||||
float x, y, z;
|
||||
@@ -63,13 +67,24 @@ typedef struct tagTRIANGLE
|
||||
typedef struct tagSECTOR
|
||||
{
|
||||
int numtriangles;
|
||||
TRIANGLE* triangle;
|
||||
TRIANGLE *triangle;
|
||||
} SECTOR;
|
||||
|
||||
// Our Model Goes Here:
|
||||
SECTOR sector1 = { .numtriangles = 0, .triangle = NULL };
|
||||
typedef struct tagAPPSTATE
|
||||
{
|
||||
SDL_Window *win;
|
||||
SDL_GLContext ctx;
|
||||
|
||||
void readstr(FILE *f, char *string)
|
||||
bool fullscreen, blend;
|
||||
|
||||
CAMERA camera;
|
||||
unsigned filter; // Filtered texture selection
|
||||
GLuint texture[3]; // Filtered textures
|
||||
|
||||
SECTOR sector1;
|
||||
} APPSTATE;
|
||||
|
||||
static void readstr(FILE *f, char *string)
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -78,37 +93,37 @@ void readstr(FILE *f, char *string)
|
||||
return;
|
||||
}
|
||||
|
||||
void SetupWorld(void)
|
||||
static void SetupWorld(APPSTATE *state)
|
||||
{
|
||||
float x, y, z, u, v;
|
||||
int numtriangles;
|
||||
FILE *filein;
|
||||
char oneline[255];
|
||||
filein = fopen("data/world.txt", "r"); // File To Load World Data From
|
||||
filein = fopen("data/world.txt", "r"); // File to load world data from
|
||||
|
||||
readstr(filein, oneline);
|
||||
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
|
||||
|
||||
sector1.triangle = malloc(sizeof(TRIANGLE) * numtriangles);
|
||||
sector1.numtriangles = numtriangles;
|
||||
state->sector1.triangle = malloc(sizeof(TRIANGLE) * numtriangles);
|
||||
state->sector1.numtriangles = numtriangles;
|
||||
for (int loop = 0; loop < numtriangles; loop++)
|
||||
{
|
||||
for (int vert = 0; vert < 3; vert++)
|
||||
{
|
||||
readstr(filein, oneline);
|
||||
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
|
||||
sector1.triangle[loop].vertex[vert].x = x;
|
||||
sector1.triangle[loop].vertex[vert].y = y;
|
||||
sector1.triangle[loop].vertex[vert].z = z;
|
||||
sector1.triangle[loop].vertex[vert].u = u;
|
||||
sector1.triangle[loop].vertex[vert].v = v;
|
||||
state->sector1.triangle[loop].vertex[vert].x = x;
|
||||
state->sector1.triangle[loop].vertex[vert].y = y;
|
||||
state->sector1.triangle[loop].vertex[vert].z = z;
|
||||
state->sector1.triangle[loop].vertex[vert].u = u;
|
||||
state->sector1.triangle[loop].vertex[vert].v = v;
|
||||
}
|
||||
}
|
||||
fclose(filein);
|
||||
return;
|
||||
}
|
||||
|
||||
bool FlipSurface(SDL_Surface *surface)
|
||||
static bool FlipSurface(SDL_Surface *surface)
|
||||
{
|
||||
if (!surface || SDL_LockSurface(surface) < 0)
|
||||
{
|
||||
@@ -138,17 +153,17 @@ bool FlipSurface(SDL_Surface *surface)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadGLTextures(void) // Load bitmap as textures
|
||||
static bool LoadGLTextures(APPSTATE *state)
|
||||
{
|
||||
SDL_Surface *TextureImage = NULL; // Create Storage Space For The Texture
|
||||
SDL_Surface *TextureImage = NULL;
|
||||
|
||||
// Load & flip the bitmap, check for errors
|
||||
// Load & flip the bitmap
|
||||
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
glGenTextures(3, &texture[0]); // Create three textures
|
||||
glGenTextures(3, &state->texture[0]); // Create three textures
|
||||
GLint params[3][3] =
|
||||
{
|
||||
[0] = { GL_NEAREST, GL_NEAREST, GL_FALSE }, // Nearest filtered
|
||||
@@ -157,7 +172,7 @@ bool LoadGLTextures(void) // Load bitmap as textures
|
||||
};
|
||||
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_MIN_FILTER, params[i][1]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, params[i][2]);
|
||||
@@ -166,7 +181,8 @@ bool LoadGLTextures(void) // Load bitmap as textures
|
||||
0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage->pixels);
|
||||
}
|
||||
|
||||
SDL_DestroySurface(TextureImage); // Free the image structure
|
||||
// Free temporary surface
|
||||
SDL_DestroySurface(TextureImage);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -187,235 +203,220 @@ static void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdou
|
||||
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
|
||||
glLoadIdentity(); // Reset The Projection Matrix
|
||||
glMatrixMode(GL_PROJECTION); // Select & reset the projection matrix
|
||||
glLoadIdentity();
|
||||
|
||||
// Calculate The Aspect Ratio Of The Window
|
||||
gluPerspective(45.0f, (float)width / (float)height, 0.1f, 100.0f);
|
||||
float aspect = (float)width / (float)height; // Calculate aspect ratio
|
||||
gluPerspective(45.0f, aspect, 0.1f, 100.0f); // Setup perspective matrix
|
||||
|
||||
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
|
||||
glLoadIdentity(); // Reset The Modelview Matrix
|
||||
glMatrixMode(GL_MODELVIEW); // Select & 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;
|
||||
}
|
||||
|
||||
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
|
||||
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
|
||||
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
|
||||
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
|
||||
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
||||
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
|
||||
glEnable(GL_TEXTURE_2D); // Enable texture mapping
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set the blending function for translucency
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set the background clear color to black
|
||||
glClearDepth(1.0); // Ensure depth buffer clears to furthest value
|
||||
glDepthFunc(GL_LESS); // Pass if pixel depth value tests less than the depth buffer value
|
||||
glEnable(GL_DEPTH_TEST); // Enable depth testing
|
||||
glShadeModel(GL_SMOOTH); // Enable Smooth color shading
|
||||
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
|
||||
glLoadIdentity(); // Reset The View
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the color and depth buffers
|
||||
glLoadIdentity(); // Reset modelview
|
||||
|
||||
GLfloat x_m, y_m, z_m, u_m, v_m;
|
||||
GLfloat xtrans = -camera.xpos;
|
||||
GLfloat ztrans = -camera.zpos;
|
||||
GLfloat ytrans = -camera.walkbias - 0.25f;
|
||||
GLfloat sceneroty = 360.0f - camera.yrot;
|
||||
GLfloat xtrans = -state->camera.xpos;
|
||||
GLfloat ztrans = -state->camera.zpos;
|
||||
GLfloat ytrans = -state->camera.walkbias - 0.25f;
|
||||
GLfloat sceneroty = 360.0f - state->camera.yrot;
|
||||
|
||||
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);
|
||||
|
||||
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++)
|
||||
{
|
||||
glBegin(GL_TRIANGLES);
|
||||
glNormal3f(0.0f, 0.0f, 1.0f);
|
||||
x_m = sector1.triangle[loop_m].vertex[0].x;
|
||||
y_m = sector1.triangle[loop_m].vertex[0].y;
|
||||
z_m = sector1.triangle[loop_m].vertex[0].z;
|
||||
u_m = sector1.triangle[loop_m].vertex[0].u;
|
||||
v_m = sector1.triangle[loop_m].vertex[0].v;
|
||||
x_m = state->sector1.triangle[loop_m].vertex[0].x;
|
||||
y_m = state->sector1.triangle[loop_m].vertex[0].y;
|
||||
z_m = state->sector1.triangle[loop_m].vertex[0].z;
|
||||
u_m = state->sector1.triangle[loop_m].vertex[0].u;
|
||||
v_m = state->sector1.triangle[loop_m].vertex[0].v;
|
||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||
|
||||
x_m = sector1.triangle[loop_m].vertex[1].x;
|
||||
y_m = sector1.triangle[loop_m].vertex[1].y;
|
||||
z_m = sector1.triangle[loop_m].vertex[1].z;
|
||||
u_m = sector1.triangle[loop_m].vertex[1].u;
|
||||
v_m = sector1.triangle[loop_m].vertex[1].v;
|
||||
x_m = state->sector1.triangle[loop_m].vertex[1].x;
|
||||
y_m = state->sector1.triangle[loop_m].vertex[1].y;
|
||||
z_m = state->sector1.triangle[loop_m].vertex[1].z;
|
||||
u_m = state->sector1.triangle[loop_m].vertex[1].u;
|
||||
v_m = state->sector1.triangle[loop_m].vertex[1].v;
|
||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||
|
||||
x_m = sector1.triangle[loop_m].vertex[2].x;
|
||||
y_m = sector1.triangle[loop_m].vertex[2].y;
|
||||
z_m = sector1.triangle[loop_m].vertex[2].z;
|
||||
u_m = sector1.triangle[loop_m].vertex[2].u;
|
||||
v_m = sector1.triangle[loop_m].vertex[2].v;
|
||||
x_m = state->sector1.triangle[loop_m].vertex[2].x;
|
||||
y_m = state->sector1.triangle[loop_m].vertex[2].y;
|
||||
z_m = state->sector1.triangle[loop_m].vertex[2].z;
|
||||
u_m = state->sector1.triangle[loop_m].vertex[2].u;
|
||||
v_m = state->sector1.triangle[loop_m].vertex[2].v;
|
||||
glTexCoord2f(u_m, v_m); glVertex3f(x_m, y_m, z_m);
|
||||
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_ShowCursor(); // Show Mouse Pointer
|
||||
SDL_SetWindowFullscreen(state->win, SDL_FALSE);
|
||||
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_GL_DeleteContext(ctx); // Delete The RC
|
||||
ctx = NULL; // Set RC To NULL
|
||||
SDL_GL_DeleteContext(state->ctx);
|
||||
state->ctx = NULL;
|
||||
}
|
||||
|
||||
SDL_DestroyWindow(win); // Destroy The Window
|
||||
win = NULL; // Set hWnd To NULL
|
||||
SDL_DestroyWindow(state->win);
|
||||
state->win = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* This Code Creates Our OpenGL Window. Parameters Are: *
|
||||
* title - Title To Appear At The Top Of The Window *
|
||||
* width - Width Of The GL Window Or Fullscreen Mode *
|
||||
* height - Height Of The GL Window Or Fullscreen Mode *
|
||||
* bits - Number Of Bits To Use For Color (8/16/24/32) *
|
||||
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
|
||||
|
||||
|
||||
bool CreateGLWindow(char *title, int width, int height, int bits, bool fullscreenflag)
|
||||
/* This code creates our OpenGL window, parameters are: *
|
||||
* title - Title to appear at the top of the window *
|
||||
* width - Width of the OpenGL 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) *
|
||||
* fullscreenflag - Use fullscreen mode (true) Or windowed mode (false) */
|
||||
static bool CreateGLWindow(APPSTATE *state, char *title, int width, int height, int bits, bool fullscreenflag)
|
||||
{
|
||||
SDL_Rect WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
|
||||
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
|
||||
state->fullscreen = fullscreenflag;
|
||||
|
||||
fullscreen = fullscreenflag; // Set The Global Fullscreen Flag
|
||||
|
||||
// Create The Window
|
||||
if (!(win = SDL_CreateWindow(title,
|
||||
WindowRect.w, // Window Width
|
||||
WindowRect.h, // Window Height
|
||||
if (!(state->win = SDL_CreateWindow(title, width, height,
|
||||
SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY)))
|
||||
{
|
||||
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, SDL_TRUE) < 0)
|
||||
{
|
||||
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
|
||||
const SDL_MessageBoxData msgbox =
|
||||
// If mode switching fails, ask the user to quit or use to windowed mode
|
||||
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,
|
||||
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.
|
||||
// User chose to quit
|
||||
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_MAJOR_VERSION, 1);
|
||||
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_RED_SIZE, 0); // Color Bits Ignored
|
||||
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_GREEN_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_ACCUM_ALPHA_SIZE, 0); // No Accumulation Buffer
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); // Accumulation Bits Ignored
|
||||
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_RED_SIZE, 0); // Accumulation bits ignored
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_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_STENCIL_SIZE, 0); // No Stencil 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
|
||||
|
||||
|
||||
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);
|
||||
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);
|
||||
return false; // Return FALSE
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_ShowWindow(win);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
|
||||
SDL_ShowWindow(state->win);
|
||||
SDL_GL_SetSwapInterval(1); // Enable VSync
|
||||
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);
|
||||
return false; // Return FALSE
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; // Success
|
||||
return true;
|
||||
}
|
||||
|
||||
int SDL_AppEvent(const SDL_Event *event)
|
||||
int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
||||
{
|
||||
APPSTATE *state = (APPSTATE *)appstate;
|
||||
switch (event->type)
|
||||
{
|
||||
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:
|
||||
if (event->key.keysym.sym == SDLK_ESCAPE) // Quit on Escape
|
||||
if (event->key.keysym.sym == 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?
|
||||
{
|
||||
switch (event->key.keysym.sym)
|
||||
{
|
||||
case SDLK_b: // B = Toggle blending
|
||||
blend = !blend;
|
||||
if (!blend)
|
||||
state->blend = !state->blend;
|
||||
if (!state->blend)
|
||||
{
|
||||
glDisable(GL_BLEND);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -425,164 +426,182 @@ int SDL_AppEvent(const SDL_Event *event)
|
||||
glEnable(GL_BLEND);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case SDLK_f: // F = Cycle texture filtering
|
||||
filter += 1;
|
||||
if (filter > 2)
|
||||
state->filter += 1;
|
||||
if (state->filter > 2)
|
||||
{
|
||||
filter = 0;
|
||||
state->filter = 0;
|
||||
}
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case SDLK_F1: // F1 = Toggle Fullscreen / Windowed Mode
|
||||
SDL_SetWindowFullscreen(win, !fullscreen);
|
||||
return 0;
|
||||
case SDLK_F1: // F1 = Toggle fullscreen / windowed mode
|
||||
SDL_SetWindowFullscreen(state->win, !state->fullscreen);
|
||||
break;
|
||||
|
||||
default: return 0;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: // Deal with window resizes
|
||||
ReSizeGLScene(event->window.data1, event->window.data2); // data1=Backbuffer Width, data2=Backbuffer Height
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
|
||||
fullscreen = true;
|
||||
return 0;
|
||||
state->fullscreen = true;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
|
||||
fullscreen = false;
|
||||
return 0;
|
||||
state->fullscreen = false;
|
||||
break;
|
||||
|
||||
default: return 0;
|
||||
default: break;
|
||||
}
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
int SDL_AppIterate(void)
|
||||
int SDL_AppIterate(void *appstate)
|
||||
{
|
||||
DrawGLScene(); // Draw the scene
|
||||
SDL_GL_SwapWindow(win); // Swap buffers (Double buffering)
|
||||
APPSTATE *state = (APPSTATE *)appstate;
|
||||
DrawGLScene(state); // Draw the scene
|
||||
SDL_GL_SwapWindow(state->win); // Swap buffers (double buffering)
|
||||
|
||||
// Handle keyboard input
|
||||
const Uint8* keys = SDL_GetKeyboardState(NULL);
|
||||
const Uint8 *keys = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (keys[SDL_SCANCODE_PAGEUP])
|
||||
{
|
||||
camera.z -= 0.02f;
|
||||
state->camera.z -= 0.02f;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_PAGEDOWN])
|
||||
{
|
||||
camera.z += 0.02f;
|
||||
state->camera.z += 0.02f;
|
||||
}
|
||||
|
||||
const float piover180 = 0.0174532925f;
|
||||
|
||||
if (keys[SDL_SCANCODE_UP])
|
||||
{
|
||||
camera.xpos -= sinf(camera.heading * piover180) * 0.05f;
|
||||
camera.zpos -= cosf(camera.heading * piover180) * 0.05f;
|
||||
if (camera.walkbiasangle >= 359.0f)
|
||||
state->camera.xpos -= sinf(state->camera.heading * piover180) * 0.05f;
|
||||
state->camera.zpos -= cosf(state->camera.heading * piover180) * 0.05f;
|
||||
if (state->camera.walkbiasangle >= 359.0f)
|
||||
{
|
||||
camera.walkbiasangle = 0.0f;
|
||||
state->camera.walkbiasangle = 0.0f;
|
||||
}
|
||||
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])
|
||||
{
|
||||
camera.xpos += sinf(camera.heading * piover180) * 0.05f;
|
||||
camera.zpos += cosf(camera.heading * piover180) * 0.05f;
|
||||
if (camera.walkbiasangle <= 1.0f)
|
||||
state->camera.xpos += sinf(state->camera.heading * piover180) * 0.05f;
|
||||
state->camera.zpos += cosf(state->camera.heading * piover180) * 0.05f;
|
||||
if (state->camera.walkbiasangle <= 1.0f)
|
||||
{
|
||||
camera.walkbiasangle = 359.0f;
|
||||
state->camera.walkbiasangle = 359.0f;
|
||||
}
|
||||
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])
|
||||
{
|
||||
camera.heading -= 1.0f;
|
||||
camera.yrot = camera.heading;
|
||||
state->camera.heading -= 1.0f;
|
||||
state->camera.yrot = state->camera.heading;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_LEFT])
|
||||
{
|
||||
camera.heading += 1.0f;
|
||||
camera.yrot = camera.heading;
|
||||
state->camera.heading += 1.0f;
|
||||
state->camera.yrot = state->camera.heading;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_PAGEUP])
|
||||
{
|
||||
camera.lookupdown -= 1.0f;
|
||||
state->camera.lookupdown -= 1.0f;
|
||||
}
|
||||
|
||||
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[])
|
||||
int SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
// Ask The User Which Screen Mode They Prefer
|
||||
const SDL_MessageBoxData msgbox =
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
{
|
||||
/* flags */ SDL_MESSAGEBOX_INFORMATION,
|
||||
/* 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
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
camera = (CAMERA)
|
||||
APPSTATE *state = *appstate = malloc(sizeof(APPSTATE));
|
||||
if (!state)
|
||||
{
|
||||
.heading = 0.0f,
|
||||
.xpos = 0.0f,
|
||||
.zpos = 0.0f,
|
||||
.yrot = 0.0f,
|
||||
.walkbias = 0.0f,
|
||||
.walkbiasangle = 0.0f,
|
||||
.lookupdown = 0.0f,
|
||||
.z = 0.0f
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
*state = (APPSTATE)
|
||||
{
|
||||
.win = NULL,
|
||||
.ctx = NULL,
|
||||
|
||||
.fullscreen = false,
|
||||
.blend = false, // Blending off
|
||||
|
||||
.camera = (CAMERA)
|
||||
{
|
||||
.heading = 0.0f,
|
||||
.xpos = 0.0f,
|
||||
.zpos = 0.0f,
|
||||
.yrot = 0.0f,
|
||||
.walkbias = 0.0f,
|
||||
.walkbiasangle = 0.0f,
|
||||
.lookupdown = 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;
|
||||
}
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit()
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
// Shutdown
|
||||
free(sector1.triangle);
|
||||
if (ctx)
|
||||
if (appstate)
|
||||
{
|
||||
glDeleteTextures(3, texture);
|
||||
APPSTATE *state = (APPSTATE *)appstate;
|
||||
free(state->sector1.triangle);
|
||||
if (state->ctx)
|
||||
{
|
||||
glDeleteTextures(3, state->texture);
|
||||
}
|
||||
KillGLWindow(state);
|
||||
free(state);
|
||||
}
|
||||
KillGLWindow(); // Kill The Window
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user