Browse Source

Lesson 4.2

Godzil 3 years ago
parent
commit
8b2e806daa
5 changed files with 197 additions and 146 deletions
  1. 1 1
      CMakeLists.txt
  2. 142 0
      source/display.c
  3. 1 1
      source/include/colour.h
  4. 33 0
      source/include/display.h
  5. 20 144
      source/main.c

+ 1 - 1
CMakeLists.txt

@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.1)
 # External cmake modules
 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/external/cmake ${CMAKE_MODULE_PATH})
 
-project("3DEngine")
+project("ThreeDEngine")
 
 include(GetGitRevisionDescription)
 git_describe(VERSION --tags --dirty=-dirty)

+ 142 - 0
source/display.c

@@ -0,0 +1,142 @@
+/*
+ * 3D Engine 
+ * display.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 01/03/2021.
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+
+#include <SDL.h>
+
+#include <log.h>
+#include <display.h>
+
+/***********************************************************************************************************************
+ * Global variables
+ **********************************************************************************************************************/
+
+SDL_Window *window = NULL;
+SDL_Renderer *renderer = NULL;
+bool isRunning = false;
+uint32_t *frameBuffer = NULL;
+SDL_Texture *frameBufferTexture = NULL;
+int windowWidth = 800;
+int windowHeight = 600;
+
+/***********************************************************************************************************************
+ * Functions
+ **********************************************************************************************************************/
+
+bool initialiseWindow(bool fullScreen)
+{
+    bool ret = false;
+    int windowFlags = 0;
+
+    if (SDL_Init(SDL_INIT_EVERYTHING))
+    {
+        log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
+        goto exit;
+    }
+
+    log(TLOG_DEBUG, NULL, "SDL properly initialised!");
+
+    if (fullScreen)
+    {
+        log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
+        SDL_DisplayMode displayMode;
+        SDL_GetCurrentDisplayMode(0, &displayMode);
+
+        windowWidth = displayMode.w;
+        windowHeight = displayMode.h;
+
+        windowFlags = SDL_WINDOW_BORDERLESS;
+    }
+
+    window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight,
+                              windowFlags);
+    if (window == NULL)
+    {
+        log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
+        goto exit;
+    }
+    log(TLOG_DEBUG, NULL, "SDL Window created!");
+
+    renderer = SDL_CreateRenderer(window, -1, 0);
+    if (renderer == NULL)
+    {
+        log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
+        goto exit;
+    }
+    log(TLOG_DEBUG, NULL, "SDL Renderer created!");
+
+    if (fullScreen)
+    {
+        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
+    }
+
+    ret = true;
+
+exit:
+    return ret;
+}
+
+void destroy_window()
+{
+    if (frameBuffer != NULL)
+    {
+        free(frameBuffer);
+    }
+    SDL_DestroyRenderer(renderer);
+    SDL_DestroyWindow(window);
+    SDL_Quit();
+}
+
+void clearFrameBuffer(uint32_t colour)
+{
+    int x, y;
+    for (y = 0 ; y < windowHeight ; y++)
+    {
+        for (x = 0 ; x < windowWidth ; x++)
+        {
+            frameBuffer[x + (y * windowWidth)] = colour;
+        }
+    }
+}
+
+void drawGrid(int spacing, uint32_t colour)
+{
+    int x, y;
+    for (y = 0 ; y < windowHeight ; y++)
+    {
+        for (x = 0 ; x < windowWidth ; x++)
+        {
+            if (((x % spacing) == 0) || ((y % spacing) == 0))
+            {
+                frameBuffer[x + (y * windowWidth)] = colour;
+            }
+        }
+    }
+}
+
+void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colour)
+{
+    uint32_t i, j;
+
+    for (j = 0 ; j < h ; j++)
+    {
+        for (i = 0 ; i < w ; i++)
+        {
+            frameBuffer[i + x + ((j + y) * windowWidth)] = colour;
+        }
+    }
+}
+
+void renderFrameBuffer()
+{
+    SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t));
+    SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
+}

+ 1 - 1
source/include/colour.h

@@ -38,5 +38,5 @@
 
 #define CNORMAL ANSI_COLOR("0")
 
-#endif	/* COLOR_H */
+#endif	/* LOG_COLOUR_H */
 

+ 33 - 0
source/include/display.h

@@ -0,0 +1,33 @@
+/*
+ * 3D Engine 
+ * display.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 01/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
+#define THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
+
+#include <SDL.h>
+
+extern SDL_Window *window;
+extern SDL_Renderer *renderer;
+extern bool isRunning;
+extern uint32_t *frameBuffer;
+extern SDL_Texture *frameBufferTexture;
+extern int windowWidth;
+extern int windowHeight;
+
+//
+
+bool initialiseWindow(bool fullScreen);
+
+void destroy_window();
+
+void clearFrameBuffer(uint32_t colour);
+void drawGrid(int spacing, uint32_t colour);
+void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colour);
+void renderFrameBuffer();
+#endif /* THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H */

+ 20 - 144
source/main.c

@@ -15,69 +15,7 @@
 
 #include <log.h>
 
-SDL_Window *window = NULL;
-SDL_Renderer *renderer = NULL;
-bool isRunning = false;
-uint32_t *frameBuffer = NULL;
-SDL_Texture *frameBufferTexture = NULL;
-int windowWidth = 800;
-int windowHeight = 600;
-
-#define color_buffer frameBuffer;
-
-bool initialiseWindow(bool fullScreen)
-{
-    bool ret = false;
-    int windowFlags = 0;
-
-    if (SDL_Init(SDL_INIT_EVERYTHING))
-    {
-        log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
-        goto exit;
-    }
-
-    log(TLOG_DEBUG, NULL, "SDL properly initialised!");
-
-    if (fullScreen)
-    {
-        log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
-        SDL_DisplayMode displayMode;
-        SDL_GetCurrentDisplayMode(0, &displayMode);
-
-        windowWidth = displayMode.w;
-        windowHeight = displayMode.h;
-
-        windowFlags = SDL_WINDOW_BORDERLESS;
-    }
-
-    window = SDL_CreateWindow(NULL,
-                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
-                              windowWidth, windowHeight,
-                              windowFlags);
-    if (window == NULL)
-    {
-        log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
-        goto exit;
-    }
-    log(TLOG_DEBUG, NULL, "SDL Window created!");
-
-    renderer = SDL_CreateRenderer(window, -1, 0);
-    if (renderer == NULL)
-    {
-        log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
-        goto exit;
-    }
-    log(TLOG_DEBUG, NULL, "SDL Renderer created!");
-
-    if (fullScreen)
-    {
-        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
-    }
-
-    ret = true;
-exit:
-    return ret;
-}
+#include <display.h>
 
 void setup()
 {
@@ -88,10 +26,8 @@ void setup()
         goto exit;
     }
 
-    frameBufferTexture = SDL_CreateTexture(renderer,
-                                           SDL_PIXELFORMAT_ARGB8888,
-                                           SDL_TEXTUREACCESS_STREAMING,
-                                           windowWidth, windowHeight);
+    frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, windowWidth,
+                                           windowHeight);
     if (frameBufferTexture == NULL)
     {
         log(TLOG_PANIC, NULL, "SDL Texture creation error: %s", SDL_GetError());
@@ -107,18 +43,17 @@ void processInput()
     SDL_Event event;
     SDL_PollEvent(&event);
 
-    switch(event.type)
+    switch (event.type)
     {
-    case SDL_QUIT:
-        isRunning = false;
+    case SDL_QUIT:isRunning = false;
         break;
 
-     case SDL_KEYDOWN:
-         if (event.key.keysym.sym == SDLK_ESCAPE)
-         {
-             isRunning = false;
-         }
-         break;
+    case SDL_KEYDOWN:
+        if (event.key.keysym.sym == SDLK_ESCAPE)
+        {
+            isRunning = false;
+        }
+        break;
 
     default:
         break;
@@ -131,52 +66,6 @@ void update()
 
 }
 
-void clearFrameBuffer(uint32_t colour)
-{
-    int x, y;
-    for(y = 0; y < windowHeight; y++)
-    {
-        for(x = 0; x < windowWidth; x++)
-        {
-            frameBuffer[x + (y * windowWidth)] = colour;
-        }
-    }
-}
-
-void drawGrid(int spacing, uint32_t colour)
-{
-    int x, y;
-    for(y = 0; y < windowHeight; y++)
-    {
-        for(x = 0; x < windowWidth; x++)
-        {
-            if (((x % spacing) == 0) || ((y % spacing) == 0))
-            {
-                frameBuffer[x + (y * windowWidth)] = colour;
-            }
-        }
-    }
-}
-
-void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colour)
-{
-    uint32_t i, j;
-
-    for(j = 0; j < h; j++)
-    {
-        for(i = 0; i < w; i++)
-        {
-            frameBuffer[i + x + ((j + y) * windowWidth)] = colour;
-        }
-    }
-}
-
-void renderFrameBuffer()
-{
-    SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, windowWidth * sizeof(uint32_t));
-    SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
-}
-
 void render()
 {
     SDL_SetRenderDrawColor(renderer, 255, 64, 13, 255);
@@ -188,23 +77,10 @@ void render()
 
     renderFrameBuffer();
     clearFrameBuffer(0xFF000000);
-
-
+    
     SDL_RenderPresent(renderer);
 }
 
-
-void destroy_window()
-{
-    if (frameBuffer != NULL)
-    {
-        free(frameBuffer);
-    }
-    SDL_DestroyRenderer(renderer);
-    SDL_DestroyWindow(window);
-    SDL_Quit();
-}
-
 int main(int argc, char *argv[])
 {
     int param_i;
@@ -213,18 +89,18 @@ int main(int argc, char *argv[])
 
     log(TLOG_ALWAYS, NULL, "Booting 3D Engine (version %s)!", VERSION);
 
-    for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-'); param_i++)
+    for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-') ; param_i++)
     {
         switch (argv[param_i][1])
         {
-            default: exit(-1); /* Option not recognized */
-            case 'f': fullScreen = true; break;
-            case 'w': windowWidth = atoi(argv[++param_i]); break;
-            case 'h': windowHeight = atoi(argv[++param_i]); break;
+        default: exit(-1); /* Option not recognized */
+        case 'f': fullScreen = true; break;
+        case 'w': windowWidth = atoi(argv[++param_i]);
+        case 'h': windowHeight = atoi(argv[++param_i]); break;
 #ifdef DYNA_LOG_LEVEL
-            case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break;
+        case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break;
 #endif
-            case '-': goto no_more_params;
+        case '-': goto no_more_params; /* Could use break, but this is more clear */
         }
     }
 no_more_params:
@@ -233,7 +109,7 @@ no_more_params:
 
     setup();
 
-    while(isRunning)
+    while (isRunning)
     {
         processInput();
         update();