Basic renderer in place. Only up from here.

git-svn-id: https://svn.tlawal.org/svn/ragar@5 618e81bd-0769-42e7-8608-769e73fecc77
This commit is contained in:
Tijani Lawal 2024-08-02 18:03:59 +00:00
parent 77a94504ea
commit b658880db8
8 changed files with 304 additions and 1 deletions

71
src/base/base_arena.h Normal file
View File

@ -0,0 +1,71 @@
#ifndef BASE_ARENA_H
#define BASE_ARENA_H
#define ARENA_HEADER_SIZE 128
#ifndef ARENA_RESERVE_SIZE
#define ARENA_RESERVE_SIZE MB(64)
#endif
#ifndef ARENA_COMMIT_SIZE
#define ARENA_COMMIT_SIZE KB(64)
#endif
#ifndef ARENA_RESERVE_SIZE_LARGE_PAGES
#define ARENA_RESERVE_SIZE_LARGE_PAGES MB(8)
#endif
#ifndef ARENA_COMMIT_SIZE_LARGE_PAGES
#define ARENA_COMMIT_SIZE_LARGE_PAGES MB(2)
#endif
// Arena
typedef struct Arena Arena;
struct Arena {
struct Arena *previous;
struct Arena *current;
u64 base_position;
u64 position;
u64 commit;
u64 reserve;
u64 align;
b8 grow;
b8 large_pages;
};
typedef struct Temp Temp;
struct Temp {
Arena *arena;
u64 position;
};
// Implementation
internal Arena *arena_alloc__sized(u64 int_res, u64 init_cmt); // cmt? res?
internal Arena *arena_alloc(void);
internal void arena_release(Arena *arena);
internal void *arena_push__impl(Arena *arena, u64 size);
internal u64 arena_position(Arena *arena);
internal void arena_pop_to(Arena *arena, u64 position);
internal void arena_absorb(Arena *arena, Arena *sub);
// Wrapper functions
internal void *arena_push(Arena *arena, u64 size);
internal void *arena_push_contiguous(Arena *arena, u64 size);
internal void arena_clear(Arena *arena);
internal void arena_push_align(Arena *arena, u64 align);
internal void arena_put_back(Arena *arena, u64 amount);
internal Temp temp_begin(Arena *arena);
internal void temp_end(Temp temp);
internal b32 ensure_commit(void **commit, void *position, u64 commit_block_size);
// Main API Macros
#define push_array_no_zero(arena, Type, commit) (Type *)arena_push((arena), sizeof(Type) * (commit))
#define push_array(arena, Type, commit) \
(Type *)MemoryZero(push_array_no_zero(arena, Type, commit), sizeof(Type) * (commit))
#endif // BASE_ARENA_H

39
src/base/base_core.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef BASE_TYPES_H
#define BASE_TYPES_H
#include <stdint.h>
// Keywords Macros
#define internal static
#define global static
#define local static
// Base Types
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef s8 b8;
typedef s16 b16;
typedef s32 b32;
typedef s64 b64;
typedef float f32;
typedef double f64;
// Units
#define KB(n) (((u64)(n)) << 10)
#define MB(n) (((u64)(n)) << 20)
#define GB(n) (((u64)(n)) << 30)
#define TB(n) (((u64)(n)) << 40)
#define Thousand(n) ((n) * 1000)
#define Million(n) ((n) * 1000000)
#define Billion(n) ((n) * 1000000000)
#endif // BASE_TYPES_H

11
src/base/base_inc.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef BASE_INC_H
#define BASE_INC_H
// clang-format off
// #include "base_context_switching.h"
#include "base_core.h"
// clang-format on
#endif // BASE_INC_H

17
src/base/base_profile.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef BASE_PROFILE_H
#define BASE_PROFILE_H
#if !defined(PROFILE_TRACY)
#define PROFILE_TRACY 0
#endif
// Third Party Includes
#if PROFILE_TRACY
#include "third_party/tracy/tracy/TracyC.h"
#endif
#if PROFILE_TRACY
#define FrameStart(...)
#endif
#endif // BASE_PROFILE_H

66
src/ragar/ragar_main.c Normal file
View File

@ -0,0 +1,66 @@
#define WIN32_LEAN_AND_MEAN
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "base/base_inc.h"
#include "renderer/display.h"
#include "renderer/display.c"
int is_running;
void setup(void) {
colour_buffer = (u32 *)malloc(sizeof(u32) * window_width * window_height);
colour_buffer_texture =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, window_width, window_height);
}
void process_input(void) {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
is_running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
is_running = false;
} else if (event.key.keysym.sym == SDLK_q) {
is_running = false;
}
break;
}
}
void update(void) {}
void render(void) {
// FrameMarkStart("render");
SDL_SetRenderDrawColor(renderer, 28, 450, 560, 255);
SDL_RenderClear(renderer);
draw_grid(0xFFFFFFFF);
draw_rect(300, 250, 300, 150, 0xFF435997);
render_colour_buffer();
// clear_colour_buffer(0xFF384893);
clear_colour_buffer(0xFF000000);
SDL_RenderPresent(renderer);
}
int main(int argc, char **argv) {
is_running = initialize_window();
setup();
while (is_running) {
process_input();
update();
render();
}
destroy_window();
return 0;
}

77
src/renderer/display.c Normal file
View File

@ -0,0 +1,77 @@
int initialize_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "Error initializing SDL.\n");
return false;
}
// NOTE(tijani): Query screen size for fullscreen
// SDL_DisplayMode display_mode;
// SDL_GetCurrentDisplayMode(0, &display_mode); // NOTE(tijani): second screen instead of main one
// window_width = display_mode.w;
// window_height = display_mode.h;
window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width,
window_height, 0);
if (!window) {
fprintf(stderr, "SDL could not create a window.\n");
return false;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fprintf(stderr, "SDL could not create a renderer.\n");
return false;
}
// SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
return true;
}
void destroy_window(void) {
free(colour_buffer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void clear_colour_buffer(u32 colour) {
for (int i = 0; i < window_height; i++) {
for (int j = 0; j < window_width; j++) {
colour_buffer[(window_width * i) + j] = colour;
}
}
}
void render_colour_buffer(void) {
SDL_UpdateTexture(colour_buffer_texture, NULL, colour_buffer, (window_width * sizeof(u32)));
SDL_RenderCopy(renderer, colour_buffer_texture, NULL, NULL);
}
void draw_dots(u32 colour) {
for (s32 column = 0; column < window_height; column += 10) {
for (s32 row = 0; row < window_width; row += 10) {
colour_buffer[(window_width * column) + row] = colour;
}
}
}
void draw_grid(u32 colour) {
for (s32 column = 0; column < window_height; column++) {
for (s32 row = 0; row < window_width; row++) {
if ((row % 50) == 0 || (column % 100) == 0) {
colour_buffer[(window_width * column) + row] = colour;
}
}
}
}
void draw_rect(s32 x, s32 y, s32 width, s32 height, s32 colour) {
for (int row = 0; row < width; row++) {
for (int column = 0; column < height; column++) {
s32 l_row = row + x;
s32 l_column = column + y;
colour_buffer[(window_width * l_column) + l_row] = colour;
}
}
}

22
src/renderer/display.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef RAGAR_DISPLAY_H
#define RAGAR_DISPLAY_H
#include <SDL.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *colour_buffer_texture = NULL;
u32 *colour_buffer;
s32 window_width = 800;
s32 window_height = 600;
int initialize_window(void);
void destroy_window(void);
void clear_colour_buffer(u32 colour);
void render_colour_buffer(void);
void draw_grid(u32 colour);
void draw_dots(u32 colour);
void draw_rect(s32 x, s32 y, s32 width, s32 height, s32 colour);
#endif // RAGAR_DISPLAY_H