Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) 2023 Jonathan Metzgar
- *
- * This software is released under the MIT License.
- * https://opensource.org/licenses/MIT
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include "object.h"
- #include "goldfish.h"
- #define SDL_MAIN_HANDLED
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_opengl.h>
- const char *vertSource = "#version 300 es\n"
- "uniform mat4 WorldMatrix;\n"
- "uniform mat4 CameraMatrix;\n"
- "uniform mat4 ProjectionMatrix;\n"
- "\n"
- "attribute vec4 aPosition;\n"
- "attribute vec3 aNormal;\n"
- "attribute vec4 aColor;\n"
- "attribute vec4 aTexCoord;\n"
- "\n"
- "varying vec4 VS_Position;\n"
- "varying vec3 VS_Normal;\n"
- "varying vec4 VS_Color;\n"
- "varying vec4 VS_TexCoord;\n"
- "varying vec3 VS_CameraDir;\n"
- "\n"
- "void main(void)\n"
- "{\n"
- " VS_Position = WorldMatrix * aPosition;\n"
- " VS_CameraDir = CameraMatrix[3].xyz - VS_Position.xyz;\n"
- " VS_Normal = aNormal;\n"
- " VS_Color = aColor;\n"
- " VS_TexCoord = aTexCoord;\n"
- " gl_Position = ProjectionMatrix * CameraMatrix * WorldMatrix * aPosition;\n"
- "}\n";
- const char *fragSource = "\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = vec4(1, 1, 0, 1);\n"
- "}\n";
- void LogSDLError()
- {
- fprintf(stderr, "Error: %s\n", SDL_GetError());
- }
- /////// Game Engine //////
- typedef struct GameEngine
- {
- Object_t self;
- SDL_Window *window;
- int frameCount;
- double previousTime;
- double currentTime;
- double deltaTime;
- int width;
- int height;
- bool finished;
- GfPipelineState pso;
- } GameEngine;
- typedef GameEngine *GameEnginePtr;
- GameEnginePtr CreateGameEngine()
- {
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
- {
- LogSDLError();
- return NULL;
- }
- const int windowWidth = 1280;
- const int windowHeight = 720;
- // Tell SDL2 to make a window with an OpenGL context.
- SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window *window = SDL_CreateWindow("Game Engine",
- SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- windowWidth, windowHeight, windowFlags);
- if (window == NULL)
- {
- LogSDLError();
- return NULL;
- }
- // Tell SDL2 what to have in the OpenGL context.
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
- // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- SDL_GLContext glContext = SDL_GL_CreateContext(window);
- if (glContext == NULL)
- {
- LogSDLError();
- return NULL;
- }
- // Enable vsync on the display.
- SDL_GL_SetSwapInterval(1);
- SDL_GL_MakeCurrent(window, glContext);
- GameEnginePtr engine = (GameEnginePtr)allocObject(sizeof(GameEngine));
- engine->window = window;
- engine->width = windowWidth;
- engine->height = windowHeight;
- engine->currentTime = SDL_GetTicks64() / 1000.0;
- // engine->pso = GfNewPipeline(vertSource, fragSource);
- engine->pso = GfNewPipeline(NULL, fragSource);
- return engine;
- }
- void EngineStartFrame(GameEnginePtr engine)
- {
- engine->previousTime = engine->currentTime;
- engine->currentTime = SDL_GetTicks64() / 1000.0;
- engine->deltaTime = engine->currentTime - engine->previousTime;
- engine->frameCount++;
- }
- void EnginePollEvents(GameEnginePtr engine)
- {
- // Process all events for the current frame.
- SDL_Event e;
- while (SDL_PollEvent(&e))
- {
- if (e.type == SDL_KEYDOWN)
- {
- if (e.key.keysym.sym == SDLK_ESCAPE)
- engine->finished = true;
- }
- else if (e.type == SDL_WINDOWEVENT)
- {
- if (e.window.event == SDL_WINDOWEVENT_RESIZED)
- {
- engine->width = e.window.data1;
- engine->height = e.window.data2;
- }
- }
- else if (e.type == SDL_QUIT)
- {
- engine->finished = true;
- }
- else
- {
- // This is an unhandled event.
- }
- }
- }
- void EngineTick(GameEnginePtr engine)
- {
- }
- void EngineDraw(GameEnginePtr engine)
- {
- // Set up OpenGL drawing commands.
- glViewport(0, 0, engine->width, engine->height);
- double s = sin(engine->currentTime) * 0.5 + 0.5;
- glClearColor(0.1, s, 0.3, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // TODO: Put in a buffer for the shaders.
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- // TODO: Put in a buffer for our draw calls.
- // GfCommandBuffer cmdBuffer = GfCommandQueueCommandBuffer(cmdQueue);
- // GfRenderEncoder renderEncoder = GfCommandBufferRenderEncoder(cmdBuffer);
- // GfRenderEncoderSetPipeline(renderEncoder, pso);
- // GfRenderEncoderVertexBuffer(renderEncoder, triangleBuffer, 0);
- // GfRenderEncoderDrawPrimitives(renderEncoder, GfTriangles, 0, 3);
- // GfRenderEncoderEndEncoding(renderEncoder);
- // GfCommandBufferCommit();
- GfUsePipeline(engine->pso);
- // GfSetVertexBuffer(triangleBuffer, 0);
- // GfDrawPrimitives(GfTriangles, 0, 3);
- glBegin(GL_TRIANGLES);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.5f, -0.5f);
- glVertex2f(0.0f, 0.5f);
- glEnd();
- }
- void EngineEndFrame(GameEnginePtr engine)
- {
- SDL_GL_SwapWindow(engine->window);
- }
- int main(int argc, char **argv)
- {
- GameEnginePtr engine = CreateGameEngine();
- if (engine == NULL)
- {
- return -1;
- }
- // Start the game loop.
- while (!engine->finished)
- {
- EngineStartFrame(engine);
- EnginePollEvents(engine);
- // Leave the loop if we're done.
- if (engine->finished)
- break;
- EngineTick(engine);
- EngineDraw(engine);
- EngineEndFrame(engine);
- }
- releaseObject((ObjectPtr)engine);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement