Init, drawing walls semi-works
This commit is contained in:
parent
befe4a84c9
commit
09cc69d38d
8 changed files with 287 additions and 0 deletions
20
CMakeLists.txt
Normal file
20
CMakeLists.txt
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
cmake_minimum_required(VERSION 3.11...3.31)
|
||||
|
||||
project(kubo LANGUAGES C)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
set(RAYLIB_VERSION 5.5)
|
||||
find_package(raylib ${RAYLIB_VERSION} REQUIRED)
|
||||
|
||||
set(SOURCES
|
||||
kubo_wall.c
|
||||
kubo_window.c
|
||||
kubo_context.c
|
||||
main.c
|
||||
)
|
||||
|
||||
add_executable(kubo ${SOURCES})
|
||||
|
||||
target_link_libraries(kubo raylib)
|
||||
54
kubo_context.c
Normal file
54
kubo_context.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include "kubo_context.h"
|
||||
|
||||
struct kubo_context *kubo_context_init() {
|
||||
struct kubo_context *context = malloc(sizeof(struct kubo_context));
|
||||
|
||||
context->num_walls_alloc = KUBO_CONTEXT_NUM_WALLS_ALLOC;
|
||||
context->walls =
|
||||
malloc(sizeof(struct kubo_wall) * context->num_walls_alloc);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
void kubo_context_cleanup(struct kubo_context *context) {
|
||||
free(context->walls);
|
||||
free(context);
|
||||
}
|
||||
|
||||
bool kubo_context_add_wall(struct kubo_context *context,
|
||||
struct kubo_wall *wall) {
|
||||
|
||||
struct kubo_wall **new_walls;
|
||||
|
||||
if (context->num_walls == context->num_walls_alloc - 1) {
|
||||
|
||||
context->num_walls_alloc *= 2;
|
||||
|
||||
new_walls = realloc(context->walls, sizeof(struct kubo_wall *) *
|
||||
context->num_walls_alloc);
|
||||
|
||||
if (!new_walls)
|
||||
return false;
|
||||
|
||||
context->walls = new_walls;
|
||||
}
|
||||
|
||||
context->walls[context->num_walls++] = wall;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
|
||||
|
||||
if (context->num_walls > 0 &&
|
||||
(context->walls[context->num_walls - 1]->wall_state == KUBO_WALL_INIT ||
|
||||
context->walls[context->num_walls - 1]->wall_state ==
|
||||
KUBO_WALL_STARTED)) {
|
||||
return context->walls[context->num_walls - 1];
|
||||
}
|
||||
|
||||
struct kubo_wall *wall = kubo_wall_init();
|
||||
kubo_context_add_wall(context, wall);
|
||||
|
||||
return wall;
|
||||
}
|
||||
28
kubo_context.h
Normal file
28
kubo_context.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef KUBO_CONTEXT_H
|
||||
#define KUBO_CONTEXT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "kubo_wall.h"
|
||||
|
||||
#define KUBO_CONTEXT_NUM_WALLS_ALLOC 4
|
||||
|
||||
struct kubo_context {
|
||||
bool exit_pending;
|
||||
|
||||
struct kubo_wall **walls;
|
||||
unsigned int num_walls;
|
||||
unsigned int num_walls_alloc;
|
||||
};
|
||||
|
||||
struct kubo_context *kubo_context_init();
|
||||
void kubo_context_cleanup(struct kubo_context *context);
|
||||
|
||||
bool kubo_context_add_wall(struct kubo_context *context,
|
||||
struct kubo_wall *wall);
|
||||
|
||||
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context);
|
||||
|
||||
#endif
|
||||
40
kubo_wall.c
Normal file
40
kubo_wall.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "kubo_wall.h"
|
||||
|
||||
struct kubo_wall *kubo_wall_init() {
|
||||
struct kubo_wall *wall = malloc(sizeof(struct kubo_wall));
|
||||
|
||||
if (!wall)
|
||||
return NULL;
|
||||
|
||||
wall->num_vertices_alloc = KUBO_WALL_INIT_VERTICES;
|
||||
wall->vertices = malloc(sizeof(Vector2) * wall->num_vertices_alloc);
|
||||
|
||||
return wall;
|
||||
}
|
||||
|
||||
void kubo_wall_cleanup(struct kubo_wall *wall) {
|
||||
free(wall->vertices);
|
||||
free(wall);
|
||||
}
|
||||
|
||||
bool kubo_wall_append(struct kubo_wall *wall, Vector2 vertex) {
|
||||
|
||||
Vector2 *new_vertices;
|
||||
|
||||
if (wall->num_vertices == wall->num_vertices_alloc - 1) {
|
||||
|
||||
wall->num_vertices_alloc *= 2;
|
||||
|
||||
new_vertices =
|
||||
realloc(wall->vertices, sizeof(Vector2) * wall->num_vertices_alloc);
|
||||
|
||||
if (!new_vertices)
|
||||
return false;
|
||||
|
||||
wall->vertices = new_vertices;
|
||||
}
|
||||
|
||||
wall->vertices[wall->num_vertices++] = vertex;
|
||||
|
||||
return true;
|
||||
}
|
||||
28
kubo_wall.h
Normal file
28
kubo_wall.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef KUBO_WALL_H
|
||||
#define KUBO_WALL_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <raylib.h>
|
||||
|
||||
#define KUBO_WALL_INIT_VERTICES 4
|
||||
|
||||
enum kubo_wall_state {
|
||||
KUBO_WALL_INIT,
|
||||
KUBO_WALL_STARTED,
|
||||
KUBO_WALL_DONE
|
||||
};
|
||||
|
||||
struct kubo_wall {
|
||||
Vector2 *vertices;
|
||||
unsigned int num_vertices;
|
||||
unsigned int num_vertices_alloc;
|
||||
|
||||
enum kubo_wall_state wall_state;
|
||||
};
|
||||
|
||||
struct kubo_wall *kubo_wall_init();
|
||||
void kubo_wall_cleanup(struct kubo_wall *wall);
|
||||
|
||||
bool kubo_wall_append(struct kubo_wall *wall, Vector2 vertex);
|
||||
|
||||
#endif
|
||||
74
kubo_window.c
Normal file
74
kubo_window.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include "kubo_window.h"
|
||||
|
||||
void kubo_window_init(struct kubo_context *context) {
|
||||
InitWindow(KUBO_WINDOW_WIDTH, KUBO_WINDOW_HEIGHT, "Kubo");
|
||||
}
|
||||
|
||||
void kubo_window_cleanup(struct kubo_context *context) { CloseWindow(); }
|
||||
|
||||
bool kubo_window_should_close(struct kubo_context *context) {
|
||||
return context->exit_pending || WindowShouldClose();
|
||||
}
|
||||
|
||||
void kubo_window_tick(struct kubo_context *context) {
|
||||
kubo_window_render(context);
|
||||
kubo_window_input(context);
|
||||
}
|
||||
|
||||
Vector2 start;
|
||||
Vector2 end;
|
||||
int state;
|
||||
|
||||
void kubo_window_render(struct kubo_context *context) {
|
||||
BeginDrawing();
|
||||
ClearBackground(WHITE);
|
||||
|
||||
if (state == 1) {
|
||||
Vector2 mouse_pos = GetMousePosition();
|
||||
Vector2 points[] = {start, mouse_pos};
|
||||
DrawSplineLinear(points, 2, 10.f, BLACK);
|
||||
} else if (state == 2) {
|
||||
Vector2 mouse_pos = GetMousePosition();
|
||||
Vector2 points[] = {start, end};
|
||||
DrawSplineLinear(points, 2, 10.f, BLACK);
|
||||
}
|
||||
|
||||
for (int i = 0; i < context->num_walls; i++) {
|
||||
if (context->walls[i]->wall_state == KUBO_WALL_STARTED) {
|
||||
Vector2 mouse_pos = GetMousePosition();
|
||||
Vector2 points[] = {context->walls[i]->vertices[0], mouse_pos};
|
||||
DrawSplineLinear(points, 2, 10.f, BLACK);
|
||||
} else if (context->walls[i]->wall_state == KUBO_WALL_DONE) {
|
||||
DrawSplineLinear(context->walls[i]->vertices,
|
||||
context->walls[i]->num_vertices, 10.f, BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
void kubo_window_input(struct kubo_context *context) {
|
||||
if (IsKeyPressed(KEY_Q)) {
|
||||
printf("q\n");
|
||||
context->exit_pending = true;
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||
|
||||
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
|
||||
|
||||
if (current_wall->wall_state == KUBO_WALL_INIT) {
|
||||
printf("new wall\n");
|
||||
Vector2 start_pos = GetMousePosition();
|
||||
kubo_wall_append(current_wall, start_pos);
|
||||
current_wall->wall_state = KUBO_WALL_STARTED;
|
||||
} else if (current_wall->wall_state == KUBO_WALL_STARTED) {
|
||||
printf("started wall\n");
|
||||
Vector2 end_pos = GetMousePosition();
|
||||
kubo_wall_append(current_wall, end_pos);
|
||||
current_wall->wall_state = KUBO_WALL_DONE;
|
||||
} else {
|
||||
printf("neither?\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
22
kubo_window.h
Normal file
22
kubo_window.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef KUBO_WINDOW_H
|
||||
#define KUBO_WINDOW_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <raylib.h>
|
||||
|
||||
#include "kubo_context.h"
|
||||
|
||||
#define KUBO_WINDOW_WIDTH 1000
|
||||
#define KUBO_WINDOW_HEIGHT 800
|
||||
|
||||
void kubo_window_init(struct kubo_context *context);
|
||||
void kubo_window_cleanup(struct kubo_context *context);
|
||||
|
||||
bool kubo_window_should_close(struct kubo_context *context);
|
||||
|
||||
void kubo_window_tick(struct kubo_context *context);
|
||||
|
||||
void kubo_window_render(struct kubo_context *context);
|
||||
void kubo_window_input(struct kubo_context *context);
|
||||
|
||||
#endif
|
||||
21
main.c
Normal file
21
main.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "kubo_context.h"
|
||||
#include "kubo_window.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
struct kubo_context *context = kubo_context_init();
|
||||
|
||||
kubo_window_init(context);
|
||||
|
||||
while (!kubo_window_should_close(context)) {
|
||||
kubo_window_tick(context);
|
||||
}
|
||||
|
||||
kubo_window_cleanup(context);
|
||||
kubo_context_cleanup(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue