commit a45ef24d5d3d2f0cb86bb3b0e759885f8431617d Author: En Yi Date: Sat Nov 23 12:25:57 2019 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4793958 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +Make* +.vscode +main +main.code-workspace \ No newline at end of file diff --git a/include/header.h b/include/header.h new file mode 100644 index 0000000..f0fa012 --- /dev/null +++ b/include/header.h @@ -0,0 +1,49 @@ +#include +#include + +#define BEZIER_POINTS 25 + +struct kinematic_obj +{ + Rectangle rect; + Vector2 velocity; + Color color; +}; + +struct squishy_square +{ + Vector2 pos; + double width; + double height; + double top_handle; + double bott_handle; + double left_handle; + double right_handle; +}; + +// Placeholder collision checking structure. Use linked list for now +// Need to implement some sort of tree structure for efficient collision checking +struct obj_node +{ + struct kinematic_obj *obj; + struct obj_node *next; +}; + +extern struct obj_node *HEAD; + +// Object functions, kinematics.c +struct kinematic_obj init_kinematic_obj(int width, int height); +void move(struct kinematic_obj *obj, Vector2 acceleration); +void set_position(struct kinematic_obj *obj, int x, int y); +bool place_meeting(struct kinematic_obj *obj, Vector2 dir); + +// Math functions, math.c +long mag(Vector2 vec); +int sign(float val); +Vector2 dir(Vector2 vec); +Vector2* three_point_beizer(Vector2 start, Vector2 mid, Vector2 end); + +// Linked list, linked_list.c + +// Squishy Square functions, squishy.c +void draw_squishy(); \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..0976f20 --- /dev/null +++ b/main.c @@ -0,0 +1,101 @@ +/******************************************************************************************* +* +* raylib [core] example - Basic 3d example +* +* Welcome to raylib! +* +* To compile example, just press F5. +* Note that compiled executable is placed in the same folder as .c file +* +* You can find all basic examples on C:\raylib\raylib\examples folder or +* raylib official webpage: www.raylib.com +* +* Enjoy using raylib. :) +* +* This example has been created using raylib 1.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "header.h" +struct obj_node *HEAD; +int PLAYER_ACCEL = 1500; +int JUMP_ACCEL = 15000; +int GRAV = 750; +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib"); + + Camera2D camera = { 0 }; + camera.offset = (Vector2){0,0}; + camera.rotation = 0.0f; + camera.zoom = 1.0f; + + struct kinematic_obj player = init_kinematic_obj(30, 30); + player.color = BLUE; + struct kinematic_obj tile = init_kinematic_obj(900, 100); + set_position(&player, 400, 300); + set_position(&tile, -50, 380); + + // TODO: get a linked list implementation + struct obj_node tile_node = {.obj=&tile, .next=NULL}; + struct obj_node player_node = {.obj=&player, .next=&tile_node}; + HEAD = &player_node; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + struct obj_node *current; + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + //UpdateCamera(&camera); + Vector2 accel = (Vector2){ + .x = PLAYER_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT)), + .y = 0 + }; + + if (!place_meeting(&player, (Vector2){0,1})){ + accel.y = GRAV; + } + accel.x -= player.velocity.x * 6.5; + + if (IsKeyDown(KEY_SPACE) && place_meeting(&player, (Vector2){0,1})) + accel.y -= JUMP_ACCEL; + move(&player, accel); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode2D(camera); + current = HEAD; + while(current){ + DrawRectangleRec(current->obj->rect, current->obj->color); + current = current->next; + } + DrawFPS(100, 100); + EndMode2D(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/obj/kinematics.c b/obj/kinematics.c new file mode 100644 index 0000000..e73f0c9 --- /dev/null +++ b/obj/kinematics.c @@ -0,0 +1,67 @@ +#include "header.h" + +struct kinematic_obj init_kinematic_obj(int width, int height){ + struct kinematic_obj obj = { + .velocity = {0.0f,0.0f}, + .rect = {0,0,width,height}, + .color = BLACK + }; + + return obj; +}; + +void set_position(struct kinematic_obj *obj, int x, int y){ + obj->rect.x = x; + obj->rect.y = y; +}; + +void move(struct kinematic_obj *obj, Vector2 acceleration){ + // Use Euler method for moving + float delta = 1.0/(float)GetFPS(); + obj->velocity.x += acceleration.x * delta; + obj->velocity.y += acceleration.y * delta; + obj->rect.x += obj->velocity.x * delta; + obj->rect.y += obj->velocity.y * delta; + //Simplistic Collision Handling for AABB, Could add coeff of restitution? + struct obj_node *current = HEAD; + Rectangle collide_rect; + while(current != NULL){ + if(current->obj != obj){ + if (CheckCollisionRecs(obj->rect, current->obj->rect)){ + collide_rect = GetCollisionRec(obj->rect, current->obj->rect); + if(collide_rect.width < collide_rect.height){ + obj->rect.x -= sign(obj->velocity.x) * collide_rect.width; + obj->velocity.x = 0; + }else{ + obj->rect.y -= sign(obj->velocity.y) * collide_rect.height; + obj->velocity.y = 0; + } + } + } + current = current->next; + } +}; + +bool place_meeting(struct kinematic_obj *obj, Vector2 dir){ + struct obj_node *current = HEAD; + Rectangle rect_check = obj->rect; + rect_check.x += dir.x; + rect_check.y += dir.y; + Rectangle collide_rect; + while(current != NULL){ + if(current->obj != obj){ + collide_rect = GetCollisionRec(rect_check, current->obj->rect); + if (collide_rect.x > 0 || collide_rect.y > 0) + return true; + } + current = current->next; + } + return false; +} + +Vector2 center(Rectangle rect){ + return (Vector2){ + .x = rect.x + rect.width/2, + .y = rect.y + rect.height/2 + }; +} diff --git a/obj/squishy.c b/obj/squishy.c new file mode 100644 index 0000000..91c477d --- /dev/null +++ b/obj/squishy.c @@ -0,0 +1,6 @@ +#include "header.h" + +void draw_squishy(struct squishy_square square){ + + //DrawTriangleFan(); +} \ No newline at end of file diff --git a/utilities/linked_list.c b/utilities/linked_list.c new file mode 100644 index 0000000..e69de29 diff --git a/utilities/math.c b/utilities/math.c new file mode 100644 index 0000000..cea806b --- /dev/null +++ b/utilities/math.c @@ -0,0 +1,28 @@ +#include "header.h" +#include + +long mag(Vector2 vec){ + return sqrt(vec.x*vec.x + vec.y*vec.y); +} + +Vector2 dir(Vector2 vec){ + long vec_mag = mag(vec); + return (Vector2){.x = vec.x/vec_mag,.y = vec.y/vec_mag}; +} + +int sign(float val){ + return (val > 0)?1:-1; +} + +Vector2* three_point_beizer(Vector2 start, Vector2 mid, Vector2 end){ + Vector2 vertices[BEZIER_POINTS]; + double t; + double t_prime; + for (int i=0;i