Vector now appears to grow safely

This commit is contained in:
Warwick 2024-02-08 10:22:52 +00:00
parent 339cd62a96
commit d8f50ed1dc
3 changed files with 60 additions and 39 deletions

View file

@ -1,7 +1,10 @@
# New Engine - Cuz starting again from scratch in a harder language is definitely the answer. # 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: ## TODO 0.1:
- [ ] Get build system working (Engine Dynamic Library and Game Executable) - [ ] Get build system working (Engine Dynamic Library and Game Executable)
- [ ] Get Vulkan renderer working. - [ ] Get Vulkan renderer working.
- [ ] Load models (Rework OpenGL Loader) - [ ] Load models (Rework OpenGL Loader)
- [ ] Run current opengl shaders - [ ] Run current opengl shaders
- [ ] Create vector library to work with vulkan

View file

@ -5,43 +5,49 @@
#include <stdio.h> #include <stdio.h>
int main() { int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { vector *vec = vec_create(sizeof(int));
printf("Failed to initialize the SDL2 library\n"); for (int x = 0; x < 100; x++) {
return -1; vec_pushback(vec, &x);
} }
vec_destroy(vec);
SDL_Window *window = // if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, // printf("Failed to initialize the SDL2 library\n");
SDL_WINDOWPOS_CENTERED, 680, 480, SDL_WINDOW_VULKAN); // return -1;
if (!window) { // }
printf("Failed to create window\n");
return -1;
}
SDL_Surface *window_surface = SDL_GetWindowSurface(window); // SDL_Window *window =
if (!window_surface) { // SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED,
printf("Failed to get the surface from the window\n"); // SDL_WINDOWPOS_CENTERED, 680, 480, SDL_WINDOW_VULKAN);
return -1; // if (!window) {
} // printf("Failed to create window\n");
// return -1;
// }
bool running = true; // SDL_Surface *window_surface = SDL_GetWindowSurface(window);
while (running) { // if (!window_surface) {
SDL_Event input; // printf("Failed to get the surface from the window\n");
while (SDL_PollEvent(&input) > 0) { // return -1;
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); // 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);
} }

View file

@ -1,5 +1,6 @@
#include "vec.h" #include "vec.h"
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -30,16 +31,27 @@ void vec_pushback(vector *vec, void *data) {
assert(vec != NULL); assert(vec != NULL);
vec_grow(vec); 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) { void vec_grow(vector *vec) {
assert(vec != NULL); assert(vec != NULL);
if (vec->size < vec->capacity) printf("capacity: %zu\n", vec->capacity);
if (vec->size < vec->capacity) {
return; return;
}
// Grow the vector by half // Grow the vector by half
vec->elements = vec->capacity += vec->capacity / 2;
realloc(vec, vec->element_size * (vec->capacity + vec->capacity / 2)); printf("new capacity: %zu\n", vec->capacity);
assert(vec != NULL); vec->elements = reallocarray(vec->elements, vec->capacity, vec->element_size);
assert(vec->elements != NULL);
} }