Separated core from maain program

This commit is contained in:
Ulysse Cura 2026-04-12 16:19:45 +02:00
parent 931a506a0d
commit 7da6337f37
16 changed files with 2 additions and 1159 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
build build
core/*

View File

@ -4,7 +4,7 @@
"name": "Default", "name": "Default",
"includePath": [ "includePath": [
"src/**/headers", "src/**/headers",
"core/headers" "core/src/headers"
] ]
} }
], ],

View File

@ -1,44 +0,0 @@
# Source files
CORE_SOURCES := \
vector2d.c \
linked_list.c \
display.c
# Compiler and flags
CORE_CC = gcc
CORE_CFLAGS = $(sdl2-config --cflags --libs) -Wall -Wextra -std=c17 -fPIC
CORE_INCLUDE_DIRS = $(CORE_DIR)/headers
CORE_LDFLAGS =
# Deduce objects
CORE_OBJECTS = $(CORE_SOURCES:%.c=$(BUILD_DIR)/$(CORE_DIR)/%.o)
# Current file nb to process
CURRENT_CORE_FILE := 0
# Build core target
build_core: core_setup count_core_build $(BUILD_DIR)/$(CORE_OUTPUT)
# Core build directories
core_setup:
@echo "Building Core (1/3)"
$(Q)mkdir -p $(dir $(CORE_OBJECTS))
# Count
count_core_build:
$(eval export TOTAL_CORE_FILES := $(shell echo $$(($$(make -n $(CORE_OBJECTS) 2>/dev/null | grep -c "Building") + 1))))
# Link core target
$(BUILD_DIR)/$(CORE_OUTPUT): $(CORE_OBJECTS)
@echo -e "[100%] $(YELLOW)Linking $(CORE_OUTPUT)$(RESET)"
$(Q)$(CORE_CC) -shared $(CORE_LDFLAGS) -o $@ $(CORE_OBJECTS)
# Build .o files
$(BUILD_DIR)/$(CORE_DIR)/%.o: $(CORE_DIR)/%.c
$(eval CURRENT_CORE_FILE := $(shell echo $$(($(CURRENT_CORE_FILE)+1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_CORE_FILE)*100/$(TOTAL_CORE_FILES)))))
@echo -e "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(CORE_CC) $(CORE_CFLAGS) $(CORE_INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@
# Source files dependencies
-include $(SOURCE_OBJECTS:.o=.d)

View File

@ -1,36 +0,0 @@
#include "display.h"
#include <SDL2/SDL.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
void display_init(void)
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("2D_Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_WIDTH, DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
void display_update(void)
{
}
void display_clear(void)
{
SDL_SetRenderDrawColor(renderer, COLOR_WHITE);
SDL_RenderClear(renderer);
}
void display_subimage()
{
}
void display_exit_void(void)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}

View File

@ -1,17 +0,0 @@
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

View File

@ -1,24 +0,0 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <SDL2/SDL_render.h>
#define COLOR_WHITE 255, 255, 255, 255
#define DISPLAY_WIDTH 396
#define DISPLAY_HEIGHT 224
extern SDL_Window *window;
extern SDL_Renderer *renderer;
void display_init(void);
void display_update(void);
void display_clear(void);
void display_subimage();
void display_exit(void);
#endif // DISPLAY_H

View File

@ -1,13 +0,0 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <SDL2/SDL_events.h>
typedef SDL_Event event_t;
inline int pollevent(event_t *event)
{
return SDL_PollEvent(event);
}
#endif // KEYBOARD_H

View File

@ -1,8 +0,0 @@
#ifndef IMAGE_H
#define IMAGE_H
#include <SDL2/SDL.h>
typedef SDL_Texture image_t;
#endif // IMAGE_H

View File

@ -1,16 +0,0 @@
#ifndef KEYCODES_H
#define KEYCODES_H
#include <SDL2/SDL_events.h>
#define MAX_KEYCODES SDL_NUM_SCANCODES
#define KEYEV_UP SDL_KEYUP
#define KEYEV_DOWN SDL_KEYDOWN
#define KEY_UP SDL_SCANCODE_UP
#define KEY_DOWN SDL_SCANCODE_DOWN
#define KEY_LEFT SDL_SCANCODE_LEFT
#define KEY_RIGHT SDL_SCANCODE_RIGHT
#endif // KEYCODES_H

View File

@ -1,287 +0,0 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
/**
* @brief Element struct for linked lists.
* This struct is the base for storing data in a linked list.
*
* @param data Raw pointer to any type of data
* @param prev Pointer to the prev element in the linked list
* @param next Pointer to the next element in the linked list
*/
typedef struct elem_t {
void *data;
struct elem_t *prev;
struct elem_t *next;
} elem_t;
/**
* @brief A type for deleters used during the destruction of data stored in elements.
*
* During the initialisation of linked lists you can pass in argument a deleter.
* It is used during the destruction of an element.
* You can pass NULL to the initialisation to use the default deleter (kfree(elem->data)).
* Your deleter_t function declaration must look like this :
*
* void name_of_your_deleter(void *data);
*
* Then do what you need to destroy your data (don't forget to free the data itself...) but not the elements.
* You should always define your function with "inline" if it's no too long.
*/
typedef void (*deleter_t)(void *data);
/**
* @brief A type for condition used for function like linked_list_remove_if
*
* For function like linked_list_remove_if you need a condition.
* It have two argument which are "elem_t *" and "va_list", that is because it passes the element on the actual index
* and the args passed to the function.
* Your condition_t function declaration must look like this :
*
* bool name_of_your_condition(elem_t *elem, va_list);
*
* Then return true if you want the element to be removed from the list or false if not.
* You should always define your function with "static", and "inline" if the condition is not too long.
*/
typedef bool (*condition_t)(elem_t *elem, va_list args);
/**
* @brief A type for action used for function like linked_list_for_each
*
* For function like linked_list_for_each you need an action to do.
* It have two argument which are "elem_t *" and "va_list", that is because it passes the element on the actual index
* and the args passed to the function.
* Your condition_t function declaration must look like this :
*
* void name_of_your_condition(elem_t *elem, va_list);
*
* Then do whatever you want with the element data, just don't remove it.
* You should always define your function with "static", and "inline" if the action is short.
*/
typedef void (*action_t)(elem_t *elem, va_list args);
/**
* @brief Base of linked lists.
* This struct is the base for creating linked lists.
*
* @param data_size Size of the data stored in elements of the linked list (used while creating new elements)
* @param first Pointer to the first element in the linked list
* @param last Pointer to the last element in the linked list
* @param size Size of the linked list
* @param deleter Deleter of the data when removing an element
*/
typedef struct linked_list_t {
size_t data_size;
elem_t *first;
elem_t *last;
size_t size;
deleter_t elem_deleter;
} linked_list_t;
/**
* @brief Init a linked list
* This function is necessary if you create a linked list.
*
* WARNING : It doesn't allocate the memory for you !
*
* @param linked_list Pointer to an linked list
* @param data_size Data size for elements in the linked list
* @param deleter The deleter used during destroyement of an elem
*/
void linked_list_init(linked_list_t *linked_list, const size_t data_size, deleter_t deleter);
/**
* @brief Create an elem to fill with data to insert in a linked list.
* This function is usefull when you want to initialise an elem with the proper data_size strored in the linked list.
*
* WARNING : Don't change the "data" ptr after calling this function or it will lead to memory leaks !
* Instead do something like this : *(int *)elem->data = 42;
*
* @param linked_list Pointer to an linked list
*
* @return Address of initialised elem, NULL on error.
*/
elem_t *elem_create(const linked_list_t *linked_list);
/**
* @brief Destroy the given element and its data.
* This function is usefull when you want to free an element.
* The deleter is used for the data only and you can pass NULL to use the default deleter.
*
* @param elem Pointer to an element
* @param elem_deleter Function to use to free the data
*/
void destroy_elem(elem_t *elem, const deleter_t elem_deleter);
/**
* @brief Insert an element from the back in a linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param elem Pointer to an element
*/
void linked_list_push_back_elem(linked_list_t *linked_list, elem_t *elem);
/**
* @brief Insert an element from the front in a linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param elem Pointer to an element
*/
void linked_list_push_front_elem(linked_list_t *linked_list, elem_t *elem);
/**
* @brief Insert an element initialised and filled with data from the back in a linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param data Pointer to any data
*/
void linked_list_push_back(linked_list_t *linked_list, void *data);
/**
* @brief Insert an element filled with data from the front in a linked list.
*
* @param linked_list Pointer to a linked list
* @param data Pointer to any data
*/
void linked_list_push_front(linked_list_t *linked_list, void *data);
/**
* @brief Delete the last element in the given linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
*/
void linked_list_pop_back(linked_list_t *linked_list);
/**
* @brief Delete the first element in the given linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
*/
void linked_list_pop_front(linked_list_t *linked_list);
/**
* @brief Clear the given linked list, do nothing on error.
*
* @brief linked_list Pointer to a linked list
*/
void linked_list_clear(linked_list_t *linked_list);
/**
* @brief Check if the given linked list is empty.
*
* @param linked_list Pointer to a linked list
*
* @return True if the given linked list is empty else false.
*/
bool linked_list_is_empty(const linked_list_t *linked_list);
/**
* @brief Check if the given index is whithin the range of the linked list.
*
* @param linked_list Pointer to an linked_list_t
*
* @return True if index in within the range of the linked list else false.
*/
bool linked_list_is_in_bound(const linked_list_t *linked_list, const size_t index);
/**
* @brief Get element at the given index in the linked list.
*
* @param linked_list Pointer to a linked list
* @param index Index of the element
*
* @return Element, NULL on error.
*/
elem_t *linked_list_get_elem(const linked_list_t *linked_list, const size_t index);
/**
* @brief Get data in the element at the given index in the linked list.
*
* @param linked_list Pointer to an linked list
* @param index Index of the wanted element
*
* @return Data, NULL on error.
*/
void *linked_list_get(const linked_list_t *linked_list, const size_t index);
/**
* @brief Insert element at the given index in the linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param elem Pointer to an element
* @param index Index to insert element
*/
void linked_list_insert_elem(linked_list_t *linked_list, elem_t *elem, const size_t index);
/**
* @brief Insert element with data at the given index in the linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param data Data to put in the new element
* @param index Index to insert the element
*/
void linked_list_insert(linked_list_t *linked_list, void *data, const size_t index);
/**
* @brief Remove the given elem in the linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param elem Element to remove
*/
void linked_list_remove_elem(linked_list_t *linked_list, elem_t *elem);
/**
* @brief Remove element at the given index in the linked list, do nothing on error.
*
* @param linked_list Pointer to a linked list
* @param index Index of the element
*/
void linked_list_remove(linked_list_t *linked_list, const size_t index);
/**
* @brief Return first element that the condition verify
*
* @param linked_list Pointer to a linked list
* @param condition Condition to verify
* @param ... Argument to pass to the condition
*
* @return Element, NULL if no element verify the condition or if an error ocurred.
*/
elem_t *linked_list_get_elem_if(const linked_list_t *linked_list, const condition_t condition, ...);
/**
* @brief Return data in the first element that the condition verify
*
* @param linked_list Pointer to a linked list
* @param condition Condition to verify
* @param ... Argument to pass to the condition
*
* @return Element data, NULL if no element verify the condition or if an error ocurred.
*/
void *linked_list_get_if(const linked_list_t *linked_list, const condition_t condition, ...);
/**
* @brief Remove every elements that the condition verify, do nothing on error.
*
* @param linked_list Pointer to an linked list
* @param condition Condition to verify
* @param ... Argument to pass to the condition
*/
void linked_list_remove_if(linked_list_t *linked_list, const condition_t condition, ...);
/**
* @brief Apply an action to every elements in the list.
* Useful for changing a value in every single element data.
* The action can be anything that doesn't destroy the element.
*
* @param linked_list Pointer to an linked list
* @param action Action to apply
* @param ... Argument to pass to the action
*/
void linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...);
#endif // LINKED_LIST_H

View File

@ -1,6 +0,0 @@
#ifndef MALLOC_H
#define MALLOC_H
#include <stdlib.h>
#endif // MALLOC_H

View File

@ -1,87 +0,0 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* All of the functions declared in this file and *
* defined in vector2d.c are from the SDL2 source code. *
* They are just modified for float and renamed. *
* => https://github.com/libsdl-org/SDL *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <stdint.h>
#include <stdbool.h>
/**
* @brief Point struct.
*
* @param x X pos
* @param y Y pos
*/
typedef struct vector2d_t {
float x, y;
} vector2d_t;
/**
* @brief Rectangle struct.
*
* @param x X pos
* @param y Y pos
* @param w Width
* @param h Height
*/
typedef struct rect_t {
float x, y;
float w, h;
} rect_t;
#define EPSILON 0.000001f
__attribute__((const)) float fabsf(float x);
#define is_equal_to_zero(X) (fabsf(X) <= EPSILON)
#define is_not_zero(X) (fabsf(X) > EPSILON)
/**
* @brief Verify if a point is in a rectangle
*
* @param P Vector2d
* @param R Rectangle
*
* @return True if the point is in the rectangle, else false.
*/
#define point_in_rect(P, R) (((P)->x >= (R)->x) && ((P)->x < ((R)->x + (R)->w)) && \
((P)->y >= (R)->y) && ((P)->y < ((R)->y + (R)->h)))
/**
* @brief Verify if a rectangle is empty
*
* @param R Rectangle
*
* @return True if the rectangle is empty, else false.
*/
#define rect_empty(R) ((!(R)) || (is_equal_to_zero((R)->w)) || (is_equal_to_zero((R)->h)))
/**
* @brief Verify if there is a intersction between two rectangles
*
* @param A Rectangle A
* @param B Rectangle B
*
* @return True if there is an intersection, else false.
*/
bool has_intersection(const rect_t *A, const rect_t *B);
/**
* @brief Verify if there is an intersection between two rectangles and get the intersection rectangle.
*
* @param A Rectangle A
* @param B Rectangle B
* @param result The intersection rectangle
*
* @return True if there is an intersection, else false.
*/
bool intersect_rect(const rect_t *A, const rect_t *B, rect_t *result);
#endif // VECTOR2D_H

View File

@ -1,377 +0,0 @@
#include <stdarg.h>
#include "memory_alloc.h"
#include "linked_list.h"
elem_t *elem_create(const linked_list_t *linked_list)
{
elem_t *tmp;
tmp = malloc(sizeof(elem_t));
if(!tmp) return NULL;
tmp->data = malloc(linked_list->data_size);
if(!tmp->data)
{
free(tmp);
return NULL;
}
return tmp;
}
inline void destroy_elem(elem_t *elem, const deleter_t elem_deleter)
{
if(!elem) return;
if(elem_deleter)
elem_deleter(elem->data);
else
free(elem->data);
free(elem);
}
void linked_list_init(linked_list_t *linked_list, const size_t data_size, const deleter_t elem_deleter)
{
linked_list->first = NULL;
linked_list->last = NULL;
linked_list->size = 0;
linked_list->data_size = data_size;
linked_list->elem_deleter = elem_deleter;
}
void linked_list_push_back_elem(linked_list_t *linked_list, elem_t *elem)
{
if(!elem) return;
elem->prev = linked_list->last;
elem->next = NULL;
if(linked_list->last)
linked_list->last->next = elem;
else
linked_list->first = elem;
linked_list->last = elem;
linked_list->size++;
}
void linked_list_push_front_elem(linked_list_t *linked_list, elem_t *elem)
{
if(!elem) return;
elem->next = linked_list->first;
elem->prev = NULL;
if(linked_list->first)
linked_list->first->prev = elem;
else
linked_list->last = elem;
linked_list->first = elem;
linked_list->size++;
}
void linked_list_push_back(linked_list_t *linked_list, void *data)
{
elem_t *tmp = malloc(sizeof(elem_t));
if(!tmp) return;
tmp->data = data;
linked_list_push_back_elem(linked_list, tmp);
}
void linked_list_push_front(linked_list_t *linked_list, void *data)
{
elem_t *tmp = malloc(sizeof(elem_t));
if(!tmp) return;
tmp->data = data;
linked_list_push_front_elem(linked_list, tmp);
}
void linked_list_pop_back(linked_list_t *linked_list)
{
elem_t *tmp = linked_list->last;
if(!tmp) return;
linked_list->last = tmp->prev;
if(linked_list->last)
linked_list->last->next = NULL;
else
linked_list->first = NULL;
destroy_elem(tmp, linked_list->elem_deleter);
linked_list->size--;
}
void linked_list_pop_front(linked_list_t *linked_list)
{
elem_t *tmp = linked_list->first;
if(!tmp) return;
linked_list->first = tmp->next;
if(linked_list->first)
linked_list->first->prev = NULL;
else
linked_list->last = NULL;
destroy_elem(tmp, linked_list->elem_deleter);
linked_list->size--;
}
void linked_list_clear(linked_list_t *linked_list)
{
elem_t *actual_elem = linked_list->first;
elem_t *tmp;
while(actual_elem)
{
tmp = actual_elem;
actual_elem = actual_elem->next;
destroy_elem(tmp, linked_list->elem_deleter);
}
linked_list->first = NULL;
linked_list->last = NULL;
linked_list->size = 0;
}
inline bool linked_list_is_empty(const linked_list_t *linked_list)
{
return !linked_list->first;
}
inline bool linked_list_is_in_bound(const linked_list_t *linked_list, size_t index)
{
return index < linked_list->size;
}
elem_t *linked_list_get_elem(const linked_list_t *linked_list, size_t index)
{
if(!linked_list_is_in_bound(linked_list, index)) return NULL;
if(index == 0)
return linked_list->first;
if(index == linked_list->size - 1)
return linked_list->last;
elem_t *actual_elem = linked_list->first;
if(!actual_elem) return NULL;
for (size_t i = 0; i < index; i++)
{
actual_elem = actual_elem->next;
}
return actual_elem;
}
void *linked_list_get(const linked_list_t *linked_list, size_t index)
{
elem_t *tmp = linked_list_get_elem(linked_list, index);
if(!tmp) return NULL;
return tmp->data;
}
void linked_list_insert_elem(linked_list_t *linked_list, elem_t *elem, size_t index)
{
if(!elem) return;
if(!linked_list_is_in_bound(linked_list, index)) return;
if(index == 0)
{
linked_list_push_front_elem(linked_list, elem);
}
else if(index == (linked_list->size - 1))
{
linked_list_push_back_elem(linked_list, elem);
}
else
{
elem_t *next_insert_elem = linked_list_get_elem(linked_list, index);
elem_t *prev_insert_elem = next_insert_elem->prev;
elem->prev = prev_insert_elem;
elem->next = next_insert_elem;
prev_insert_elem->next = elem;
next_insert_elem->prev = elem;
linked_list->size++;
}
}
void linked_list_insert(linked_list_t *linked_list, void *data, size_t index)
{
elem_t *tmp = malloc(sizeof(elem_t));
if(!tmp) return;
tmp->data = data;
linked_list_insert_elem(linked_list, tmp, index);
}
void linked_list_remove_elem(linked_list_t *linked_list, elem_t *elem)
{
if(!elem) return;
if(elem == linked_list->first)
{
linked_list_pop_front(linked_list);
}
else if(elem == linked_list->last)
{
linked_list_pop_back(linked_list);
}
else
{
elem_t *prev_insert_elem = elem->prev;
elem_t *next_insert_elem = elem->next;
prev_insert_elem->next = next_insert_elem;
next_insert_elem->prev = prev_insert_elem;
destroy_elem(elem, linked_list->elem_deleter);
linked_list->size--;
}
}
void linked_list_remove(linked_list_t *linked_list, size_t index)
{
elem_t *tmp = linked_list_get_elem(linked_list, index);
if(tmp) linked_list_remove_elem(linked_list, tmp);
}
elem_t *linked_list_get_elem_if(const linked_list_t *linked_list, const condition_t condition, ...)
{
if(!condition) return NULL;
va_list args;
va_start(args, condition);
elem_t *actual_elem = linked_list->first;
elem_t *next_elem;
while(actual_elem)
{
next_elem = actual_elem->next;
va_list args_copy;
va_copy(args_copy, args);
if(condition(actual_elem, args_copy))
{
va_end(args_copy);
va_end(args);
return actual_elem;
}
va_end(args_copy);
actual_elem = next_elem;
}
va_end(args);
return NULL;
}
void *linked_list_get_if(const linked_list_t *linked_list, const condition_t condition, ...)
{
if(!condition) return NULL;
va_list args;
va_start(args, condition);
elem_t *actual_elem = linked_list->first;
elem_t *next_elem;
while(actual_elem)
{
next_elem = actual_elem->next;
va_list args_copy;
va_copy(args_copy, args);
if(condition(actual_elem, args_copy))
{
va_end(args_copy);
va_end(args);
return actual_elem->data;
}
va_end(args_copy);
actual_elem = next_elem;
}
va_end(args);
return NULL;
}
void linked_list_remove_if(linked_list_t *linked_list, const condition_t condition, ...)
{
if(!condition) return;
va_list args;
va_start(args, condition);
elem_t *actual_elem = linked_list->first;
elem_t *next_elem;
while(actual_elem)
{
next_elem = actual_elem->next;
va_list args_copy;
va_copy(args_copy, args);
if(condition(actual_elem, args_copy))
{
linked_list_remove_elem(linked_list, actual_elem);
}
va_end(args_copy);
actual_elem = next_elem;
}
va_end(args);
}
void linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...)
{
if(!action) return;
va_list args;
va_start(args, action);
elem_t *actual_elem = linked_list->first;
while(actual_elem)
{
va_list args_copy;
va_copy(args_copy, args);
action(actual_elem, args_copy);
va_end(args_copy);
actual_elem = actual_elem->next;
}
va_end(args);
}

View File

@ -1,97 +0,0 @@
#include "vector2d.h"
__attribute__((const)) float fabsf(float x)
{
union {
float f;
uint32_t i;
} u = {x};
u.i &= 0x7FFFFFFF;
return u.f;
}
bool has_intersection(const rect_t * A, const rect_t * B)
{
float Amin, Amax, Bmin, Bmax;
if (!A || !B)
{
return false;
}
/* Special cases for empty rects */
if (rect_empty(A) || rect_empty(B))
{
return false;
}
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return false;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return false;
return true;
}
bool intersect_rect(const rect_t * A, const rect_t * B, rect_t * result)
{
if (!A || !B || !result)
{
// TODO error message
return false;
}
/* Special cases for empty rects */
if (rect_empty(A) || rect_empty(B))
{
return false;
}
float Amin, Amax, Bmin, Bmax;
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->h = Amax - Amin;
return !rect_empty(result);
}

View File

@ -1,81 +0,0 @@
#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H
#include <image.h>
#include "vector2d.h"
#include "linked_list.h"
/* texture_t: Texture definition
This struct is the base for textures
@name Name of the texture
@image Texture of type bopti_image_t */
typedef struct texture_t {
const char *name;
image_t *image;
} texture_t;
/* create_texture(): Create a texture with the given image and name
@name Name of the texture
@image Texture of type bopti_image_t
Return the created texture, NULL on error. */
texture_t *create_texture(image_t *, const char *);
/* destroy_texture(): Destroy the given texture
@texture Texture to destroy */
void destroy_texture(texture_t *);
/* draw_texture(): Draw the given texture from given src rect to dst rect
@texture Texture to draw
@src Source rectangle of the texture to draw
@dst Destination of the drawing */
void draw_texture(const texture_t *, const rect_t *, const rect_t *, bool);
/* texture_manager_t: Texture manager
@textures Textures loaded in memory */
typedef struct texture_manager_t {
linked_list_t textures;
} texture_manager_t;
/* texture_manager_init(): Initialise the given texture_manager
@texture_manager Pointer to a texture manager */
void texture_manager_init(texture_manager_t *);
/* texture_manager_add_texture(): Add a texture in the texture manager
@texture_manager Pointer to a texture manager
@texture Texture to add in the texture manager */
void texture_manager_add_texture(texture_manager_t *, texture_t *);
/* texture_manager_get_texture(): Get a texture in the texture manager
@texture_manager Pointer to a texture manager
@name Name of the texture to get
Return the texture, NULL on error or if the texture is not found. */
texture_t *texture_manager_get_texture(texture_manager_t *, const char *);
/* texture_manager_remove_texture(): Remove a texture in the texture manager
@texture_manager Pointer to a texture manager
@name Name of the texture to remove
Do nothing if the texture is not found. */
void texture_manager_remove_texture(texture_manager_t *, const char *);
/* texture_manager_clear(): Clear the texture manager
@texture_manager Pointer to a texture manager */
void texture_manager_clear(texture_manager_t *);
/* texture_manager_draw_texture(): Draw a texture in the texture manager
@texture_manager Pointer to a texture manager
@name Name of the texture to draw
Do nothing if the texture is not found. */
void texture_manager_draw_texture(texture_manager_t *, const char *, const rect_t *, const rect_t *, bool);
#endif // TEXTURE_MANAGER_H

View File

@ -1,64 +0,0 @@
#include <malloc.h>
#include <string.h>
#include "texture_manager.h"
texture_t *create_texture(image_t *image, const char *name)
{
if(!image) return NULL;
texture_t *texture = malloc(sizeof(texture_t));
if (!texture) return NULL;
texture->name = name;
texture->image = image;
return texture;
}
inline void destroy_texture(texture_t *texture)
{
free(texture);
}
inline void draw_texture(const texture_t *texture, const rect_t *src, const rect_t *dst, bool flip)
{
display_subimage((int)dst->x, (int)dst->y, texture->image, (int)src->x, (int)src->y, (int)src->w, (int)src->h, flip);
}
inline void texture_manager_init(texture_manager_t *texture_manager)
{
linked_list_init(&texture_manager->textures, sizeof(texture_t), NULL);
}
inline void texture_manager_add_texture(texture_manager_t *texture_manager, texture_t *texture)
{
linked_list_push_back(&texture_manager->textures, texture);
}
// Only for this file
// Arg : const char *name
static inline bool is_texture_name(elem_t *elem, va_list args)
{
return !strcmp(((texture_t *)elem->data)->name, va_arg(args, const char*));
}
texture_t *texture_manager_get_texture(texture_manager_t *texture_manager, const char *name)
{
texture_t *texture = linked_list_get_if(&texture_manager->textures, is_texture_name, name);
return texture;
}
inline void texture_manager_remove_texture(texture_manager_t *texture_manager, const char *name)
{
linked_list_remove_if(&texture_manager->textures, is_texture_name, name);
}
inline void texture_manager_clear(texture_manager_t *texture_manager)
{
linked_list_clear(&texture_manager->textures);
}
inline void texture_manager_draw_texture(texture_manager_t *texture_manager, const char *name, const rect_t *src, const rect_t *dst, bool flip)
{
draw_texture(texture_manager_get_texture(texture_manager, name), src, dst, flip);
}