123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- * 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;
- uint32_t *frameBuffer = NULL;
- SDL_Texture *frameBufferTexture = NULL;
- uint32_t windowWidth = 800;
- uint32_t 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 renderFrameBuffer()
- {
- SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
- SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
- }
- void drawPixel(uint32_t x, uint32_t y, uint32_t colour)
- {
- if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
- {
- frameBuffer[x + (y * windowWidth)] = colour;
- }
- }
- void clearFrameBuffer(uint32_t colour)
- {
- uint32_t 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)
- {
- uint32_t 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++)
- {
- drawPixel(x + i, y + j, colour);
- }
- }
- }
|