Vector now appears to grow safely
This commit is contained in:
parent
339cd62a96
commit
d8f50ed1dc
3 changed files with 60 additions and 39 deletions
|
|
@ -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
|
||||
|
|
|
|||
74
src/main.c
74
src/main.c
|
|
@ -5,43 +5,49 @@
|
|||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
22
src/vec.c
22
src/vec.c
|
|
@ -1,5 +1,6 @@
|
|||
#include "vec.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue