From d8f50ed1dc3fb32353897f4189b975212e582032 Mon Sep 17 00:00:00 2001 From: Warwick Date: Thu, 8 Feb 2024 10:22:52 +0000 Subject: [PATCH] Vector now appears to grow safely --- README.md | 3 +++ src/main.c | 74 +++++++++++++++++++++++++++++------------------------- src/vec.c | 22 ++++++++++++---- 3 files changed, 60 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 01b70c1..c36f372 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # New Engine - Cuz starting again from scratch in a harder language is definitely the answer. +It's actually a pretty old style engine + ## TODO 0.1: - [ ] Get build system working (Engine Dynamic Library and Game Executable) - [ ] Get Vulkan renderer working. - [ ] Load models (Rework OpenGL Loader) - [ ] Run current opengl shaders + - [ ] Create vector library to work with vulkan diff --git a/src/main.c b/src/main.c index 62f3106..57d9884 100644 --- a/src/main.c +++ b/src/main.c @@ -5,43 +5,49 @@ #include int main() { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("Failed to initialize the SDL2 library\n"); - return -1; + vector *vec = vec_create(sizeof(int)); + for (int x = 0; x < 100; x++) { + vec_pushback(vec, &x); } + vec_destroy(vec); - SDL_Window *window = - SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, 680, 480, SDL_WINDOW_VULKAN); - if (!window) { - printf("Failed to create window\n"); - return -1; - } + // if (SDL_Init(SDL_INIT_VIDEO) < 0) { + // printf("Failed to initialize the SDL2 library\n"); + // return -1; + // } - SDL_Surface *window_surface = SDL_GetWindowSurface(window); - if (!window_surface) { - printf("Failed to get the surface from the window\n"); - return -1; - } + // SDL_Window *window = + // SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, + // SDL_WINDOWPOS_CENTERED, 680, 480, SDL_WINDOW_VULKAN); + // if (!window) { + // printf("Failed to create window\n"); + // return -1; + // } - bool running = true; - while (running) { - SDL_Event input; - while (SDL_PollEvent(&input) > 0) { - if (input.type == SDL_QUIT) { - running = false; - } - switch (input.type) { - case SDL_QUIT: - running = false; - case SDL_KEYDOWN: { - const Uint8 *keys = SDL_GetKeyboardState(NULL); - if (keys[SDL_SCANCODE_ESCAPE]) - running = false; - } - } - } - } + // SDL_Surface *window_surface = SDL_GetWindowSurface(window); + // if (!window_surface) { + // printf("Failed to get the surface from the window\n"); + // return -1; + // } - SDL_UpdateWindowSurface(window); + // bool running = true; + // while (running) { + // SDL_Event input; + // while (SDL_PollEvent(&input) > 0) { + // if (input.type == SDL_QUIT) { + // running = false; + // } + // switch (input.type) { + // case SDL_QUIT: + // running = false; + // case SDL_KEYDOWN: { + // const Uint8 *keys = SDL_GetKeyboardState(NULL); + // if (keys[SDL_SCANCODE_ESCAPE]) + // running = false; + // } + // } + // } + // } + + // SDL_UpdateWindowSurface(window); } diff --git a/src/vec.c b/src/vec.c index 9c4ee51..6e29a0c 100644 --- a/src/vec.c +++ b/src/vec.c @@ -1,5 +1,6 @@ #include "vec.h" #include +#include #include #include @@ -30,16 +31,27 @@ void vec_pushback(vector *vec, void *data) { assert(vec != NULL); vec_grow(vec); - memcpy(data, vec, vec->element_size); + printf("mem start: %p\n", vec->elements); + printf("mem end: %p\n", vec->elements + (vec->size * vec->element_size)); + printf("number of elements: %zu\n", vec->size); + printf("element size: %zu\n\n", vec->element_size); + + memcpy(vec->elements + (vec->size * vec->element_size), data, + vec->element_size); + vec->size++; } void vec_grow(vector *vec) { assert(vec != NULL); - if (vec->size < vec->capacity) + printf("capacity: %zu\n", vec->capacity); + if (vec->size < vec->capacity) { return; + } // Grow the vector by half - vec->elements = - realloc(vec, vec->element_size * (vec->capacity + vec->capacity / 2)); - assert(vec != NULL); + vec->capacity += vec->capacity / 2; + printf("new capacity: %zu\n", vec->capacity); + vec->elements = reallocarray(vec->elements, vec->capacity, vec->element_size); + + assert(vec->elements != NULL); }