Compare commits

...

2 commits

Author SHA1 Message Date
CRy386i
56d37e5170 rename math.h -> torad.h. Avoid conflict headers 2025-03-02 08:20:31 +02:00
CRy386i
46ee3f6988 fix startup render 2025-03-02 08:16:29 +02:00
6 changed files with 33 additions and 11 deletions

View file

@ -2,8 +2,8 @@ CC=gcc
CFLAGS=-O1 -DDEBUG -g -ggdb -std=c23
LIBS=`pkg-config sdl3 --cflags --libs` -lGL -lGLEW -lm
HEADER = readfile.h math.h opengl.h
OBJ = main.o readfile.o math.o opengl.o
HEADER = readfile.h torad.h opengl.h
OBJ = main.o readfile.o torad.o opengl.o
all: mobiground

16
main.c
View file

@ -15,6 +15,9 @@ static double FrameTime = 1.0 / INITIAL_FPS;
static double PerformancePeriod;
const int WIDTH = 640;
const int HEIGHT = 480;
static double GetTime( void )
{
return PerformancePeriod * SDL_GetPerformanceCounter();
@ -30,15 +33,13 @@ static void SetTitle ( SDL_Window *window )
int main( int argc, char* argv[] )
{
_glWindows_t glW;
const int WIDTH = 640;
const int HEIGHT = 480;
int delayThreshold = 2;
int r, g, b, alpha_s, depth_s, stencil_s, dbuffer, glVersionMa, glVersionMi, glAccel, glProfile = 0;
double sleepTime = 0;
double timeCounter = 0;
bool loopShouldStop = false;
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
/* SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0); */
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_Init failed: %s", SDL_GetError());
exit(EXIT_FAILURE);
@ -76,9 +77,7 @@ int main( int argc, char* argv[] )
SetTitle(glW.window);
SDL_HideCursor();
glewExperimental = GL_TRUE;
glewExperimental = GL_FALSE;
GLenum err = glewInit();
if (err != GLEW_OK) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "glewInit: %s", glewGetErrorString(err));
@ -103,8 +102,9 @@ int main( int argc, char* argv[] )
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Red size: %d, Green size: %d, Blue size: %d, Alpha size: %d, Depth size: %d, Stencil size: %d, DoubleBuffer: %d",
r, g, b, alpha_s, depth_s, stencil_s, dbuffer);
SDL_GL_SetSwapInterval(0);
SDL_GL_SetSwapInterval(1);
startShaderProg();
_matrix(WIDTH, HEIGHT);
while (!loopShouldStop) {
SDL_Event e;
@ -135,7 +135,7 @@ int main( int argc, char* argv[] )
_GLBuffer();
if (!SDL_GL_SwapWindow(glW.window)) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWwindow: %s", SDL_GetError());
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWindow: %s", SDL_GetError());
exit(EXIT_FAILURE);
}

View file

@ -7,7 +7,7 @@
#include "opengl.h"
#include "readfile.h"
#include "math.h"
#include "torad.h"
shaderProgram_t sp;
@ -33,6 +33,27 @@ void resizeWindow ( int width, int height )
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
}
void _matrix ( int width, int height )
{
float n = 1.0f; // near
float f = 3.0f; // far
float fov = toRad(90); // 90 or 65 for console
float aspect = width / (float) height;
float t = n * tanf (fov / 2.0f);
float r = t * aspect;
float projMatr[] = {
n/r, 0, 0, 0,
0, n/t, 0, 0,
0, 0, (f+n)/(n-f), -1,
0, 0, 2.0f*n*f/(n-f), 0
}; // it's not rows, it's columns, 'GL_FALSE'
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "n=%.1f f=%.1f fov=%.2f aspect=%.2f t=%.2f r=%.2f\n", n, f, fov, aspect, t, r);
glUseProgram(sp.program);
glUniformMatrix4fv(sp.projMatrLoc, 1, GL_FALSE, projMatr);
glUseProgram(0);
}
GLuint createShader ( const char *shaderFile, GLenum shaderType )
{
const char *strShaderType;

View file

@ -17,6 +17,7 @@ typedef struct {
GLuint createShader ( const char *shaderFile, GLenum shaderType );
void createProgram ( GLuint *shaders, int len );
void resizeWindow ( int width, int height );
void _matrix ( int width, int height );
void createBuffer ( void );
void startShaderProg ( void );
void _GLBuffer( void );

View file

View file