diff --git a/CMakeLists.txt b/CMakeLists.txt index b4c3116..d84acea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,21 @@ cmake_minimum_required(VERSION 3.9) project(Homework) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG") find_package(SDL2 REQUIRED) -include_directories(${SDL2_INCLUDE_DIRS}) +find_package(SDL2_TTF REQUIRED) +include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_TTF_DIRS}) -add_executable(${PROJECT_NAME} main.c Graphics.c Graphics.h) +add_executable(${PROJECT_NAME} main.c Graphics.c Graphics.h debugmalloc.h debugmalloc-impl.h) -target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2_TTF_LIBRARIES}) -set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99) \ No newline at end of file +set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99) + +add_custom_command( + TARGET ${PROJECT_NAME} PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_SOURCE_DIR}/res + $/res +) \ No newline at end of file diff --git a/Graphics.c b/Graphics.c index a88f57f..9b29323 100644 --- a/Graphics.c +++ b/Graphics.c @@ -1,6 +1,7 @@ #include "Graphics.h" #define set_pixel(dest, x, y, c) (((uint32_t *)(dest)->pixels)[(y) * dest->w + (x)] = (c)) +#define int_to_color(color) ((SDL_Color){(color) >> 24u, ((color) >> 16u) & 0xffu, ((color) >> 8u) & 0xffu, (color) & 0xffu}) //Source: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm void __gfx_draw_line_low(SDL_Surface *dest, float x0, float y0, float x1, float y1, Color color) { @@ -338,4 +339,11 @@ void gfx_draw_bezier_cubic(SDL_Surface *dest, Point V1, Point V2, Point C1, Poin prevQ1 = Q1; prevQ2 = Q2; } +} + +void gfx_draw_text(SDL_Surface *dest, TTF_Font *font, int x, int y, const char *str, Color color) { + SDL_Surface *text = TTF_RenderText_Blended(font, str, int_to_color(color)); + SDL_Rect r = {x, y, 0, 0}; + SDL_BlitSurface(text, NULL, dest, &r); + SDL_FreeSurface(text); } \ No newline at end of file diff --git a/Graphics.h b/Graphics.h index e9c3090..0354efd 100644 --- a/Graphics.h +++ b/Graphics.h @@ -1,10 +1,13 @@ #ifndef HOMEWORK_GRAPHICS_H #define HOMEWORK_GRAPHICS_H +#include "debugmalloc.h" + #include #include #include #include +#include #include #define EPSILON 0.01f @@ -27,4 +30,6 @@ void gfx_fill_circle(SDL_Surface *dest, Point C, float r, Color color); void gfx_draw_bezier(SDL_Surface *dest, Point V1, Point V2, Point C, float thickness, Color color); void gfx_draw_bezier_cubic(SDL_Surface *dest, Point V1, Point V2, Point C1, Point C2, float thickness, Color color); +void gfx_draw_text(SDL_Surface *dest, TTF_Font *font, int x, int y, const char *str, Color color); + #endif //HOMEWORK_GRAPHICS_H diff --git a/cmake/FindSDL2_ttf.cmake b/cmake/FindSDL2_ttf.cmake new file mode 100644 index 0000000..56719ce --- /dev/null +++ b/cmake/FindSDL2_ttf.cmake @@ -0,0 +1,98 @@ +# Locate SDL_ttf library +# +# This module defines: +# +# :: +# +# SDL2_TTF_LIBRARIES, the name of the library to link against +# SDL2_TTF_INCLUDE_DIRS, where to find the headers +# SDL2_TTF_FOUND, if false, do not try to link against +# SDL2_TTF_VERSION_STRING - human-readable string containing the version of SDL_ttf +# +# +# +# For backward compatibility the following variables are also set: +# +# :: +# +# SDLTTF_LIBRARY (same value as SDL2_TTF_LIBRARIES) +# SDLTTF_INCLUDE_DIR (same value as SDL2_TTF_INCLUDE_DIRS) +# SDLTTF_FOUND (same value as SDL2_TTF_FOUND) +# +# +# +# $SDLDIR is an environment variable that would correspond to the +# ./configure --prefix=$SDLDIR used in building SDL. +# +# Created by Eric Wing. This was influenced by the FindSDL.cmake +# module, but with modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). + +#============================================================================= +# Copyright 2005-2009 Kitware, Inc. +# Copyright 2012 Benjamin Eikel +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +find_path(SDL2_TTF_INCLUDE_DIR SDL_ttf.h + HINTS + ENV SDL2TTFDIR + ENV SDL2DIR + PATH_SUFFIXES SDL2 + # path suffixes to search inside ENV{SDLDIR} + include/SDL2 include + PATHS ${SDL2_TTF_PATH} + ) + +if (CMAKE_SIZEOF_VOID_P EQUAL 8) + set(VC_LIB_PATH_SUFFIX lib/x64) +else () + set(VC_LIB_PATH_SUFFIX lib/x86) +endif () + +find_library(SDL2_TTF_LIBRARY + NAMES SDL2_ttf + HINTS + ENV SDL2TTFDIR + ENV SDL2DIR + PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} + PATHS ${SDL2_TTF_PATH} + ) + +if (SDL2_TTF_INCLUDE_DIR AND EXISTS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h") + file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+[0-9]+$") + string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MAJOR "${SDL2_TTF_VERSION_MAJOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MINOR "${SDL2_TTF_VERSION_MINOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_PATCH "${SDL2_TTF_VERSION_PATCH_LINE}") + set(SDL2_TTF_VERSION_STRING ${SDL2_TTF_VERSION_MAJOR}.${SDL2_TTF_VERSION_MINOR}.${SDL2_TTF_VERSION_PATCH}) + unset(SDL2_TTF_VERSION_MAJOR_LINE) + unset(SDL2_TTF_VERSION_MINOR_LINE) + unset(SDL2_TTF_VERSION_PATCH_LINE) + unset(SDL2_TTF_VERSION_MAJOR) + unset(SDL2_TTF_VERSION_MINOR) + unset(SDL2_TTF_VERSION_PATCH) +endif () + +set(SDL2_TTF_LIBRARIES ${SDL2_TTF_LIBRARY}) +set(SDL2_TTF_INCLUDE_DIRS ${SDL2_TTF_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf + REQUIRED_VARS SDL2_TTF_LIBRARIES SDL2_TTF_INCLUDE_DIRS + VERSION_VAR SDL2_TTF_VERSION_STRING) + +# for backward compatibility +set(SDLTTF_LIBRARY ${SDL2_TTF_LIBRARIES}) +set(SDLTTF_INCLUDE_DIR ${SDL2_TTF_INCLUDE_DIRS}) +set(SDLTTF_FOUND ${SDL2_TTF_FOUND}) \ No newline at end of file diff --git a/debugmalloc-impl.h b/debugmalloc-impl.h new file mode 100644 index 0000000..003e30a --- /dev/null +++ b/debugmalloc-impl.h @@ -0,0 +1,453 @@ +#ifndef DEBUGMALLOC_IMPL_H +#define DEBUGMALLOC_IMPL_H + +#include +#include +#include +#include +#include +#include +#include + + +/* make getpid and putenv "crossplatform". deprecated on windows but they work just fine, + * however not declared. */ +#ifdef _WIN32 + /* windows */ + #include + #ifdef _MSC_VER + /* visual studio, getenv/getpid deprecated warning */ + #pragma warning(disable: 4996) + #else + /* other windows. the declaration is unfortunately hidden + * in mingw header files by ifdefs. */ + int putenv(const char *); + #endif +#else + /* posix */ + #include +#endif + + +/* size of canary in bytes. should be multiple of largest alignment + * required by any data type (usually 8 or 16) */ +enum { debugmalloc_canary_size = 64 }; + +/* canary byte */ +enum { debugmalloc_canary_char = 'K' }; + +/* hash table size for allocated entries */ +enum { debugmalloc_tablesize = 256 }; + + +/* linked list entry for allocated blocks */ +typedef struct DebugmallocElem { + void *real_mem; /* the address of the real allocation */ + void *user_mem; /* address shown to the user */ + size_t size; /* size of block requested by user */ + + char file[64]; /* malloc called in this file */ + unsigned line; /* malloc called at this line in file */ + char func[32]; /* allocation function called (malloc, calloc, realloc) */ + char expr[128]; /* expression calculating the size of allocation */ + + struct DebugmallocElem *prev, *next; /* for doubly linked list */ +} DebugmallocElem; + + +/* debugmalloc singleton, storing all state */ +typedef struct DebugmallocData { + char logfile[256]; /* log file name or empty string */ + DebugmallocElem head[debugmalloc_tablesize], tail[debugmalloc_tablesize]; /* head and tail elements of allocation lists */ +} DebugmallocData; + + +/* this forward declaration is required by the singleton manager function */ +static DebugmallocData * debugmalloc_create(void); + + +/* creates singleton instance. as this function is static included to different + * translation units, multiple instances of the static variables are created. + * to make sure it is really a singleton, these instances must know each other + * somethow. an environment variable is used for that purpose, ie. the address + * of the singleton allocated is stored by the operating system. + * this implementation is not thread-safe. */ +static DebugmallocData * debugmalloc_singleton(void) { + static char envstr[100]; + static void *instance = NULL; + + /* if we do not know the address of the singleton: + * - maybe we are the one to create it (env variable also does not exist) + * - or it is already created, and stored in the env variable. */ + if (instance == NULL) { + char envvarname[100] = ""; + sprintf(envvarname, "%s%d", "debugmallocsingleton", (int) getpid()); + char *envptr = getenv(envvarname); + if (envptr == NULL) { + /* no env variable: create singleton. */ + instance = debugmalloc_create(); + sprintf(envstr, "%s=%p", envvarname, instance); + putenv(envstr); + } else { + /* another copy of this function already created it. */ + int ok = sscanf(envptr, "%p", &instance); + if (ok != 1) { + fprintf(stderr, "debugmalloc: nem lehet ertelmezni: %s!\n", envptr); + abort(); + } + } + } + + return (DebugmallocData *) instance; +} + + +/* better version of strncpy, always terminates string with \0. */ +static void debugmalloc_strlcpy(char *dest, char const *src, size_t destsize) { + strncpy(dest, src, destsize); + dest[destsize - 1] = '\0'; +} + + +/* set the name of the log file for debugmalloc. empty filename + * means logging to stderr. */ +static void debugmalloc_log_file(char const *logfilename) { + if (logfilename == NULL) + logfilename = ""; + DebugmallocData *instance = debugmalloc_singleton(); + debugmalloc_strlcpy(instance->logfile, logfilename, sizeof(instance->logfile)); +} + + + +/* printf to the log file, or stderr. */ +static void debugmalloc_log(char const *format, ...) { + DebugmallocData *instance = debugmalloc_singleton(); + FILE *f = stderr; + if (strcmp(instance->logfile, "") != 0) { + f = fopen(instance->logfile, "at"); + if (f == NULL) { + f = stderr; + fprintf(stderr, "debugmalloc: nem tudom megnyitni a %s fajlt irasra!\n", instance->logfile); + strcpy(instance->logfile, ""); + } + } + + va_list ap; + va_start(ap, format); + vfprintf(f, format, ap); + va_end(ap); + + if (f != stderr) + fclose(f); +} + + +/* initialize a memory block allocated for the user. the start and the end + * of the block is initialized with the canary characters. if 'zero' is + * true, the user memory area is zero-initialized, otherwise it is also + * filled with the canary character to simulate garbage in memory. */ +static void debugmalloc_memory_init(DebugmallocElem *elem, bool zero) { + unsigned char *real_mem = (unsigned char *) elem->real_mem; + unsigned char *user_mem = (unsigned char *) elem->user_mem; + unsigned char *canary1 = real_mem; + unsigned char *canary2 = real_mem + debugmalloc_canary_size + elem->size; + memset(canary1, debugmalloc_canary_char, debugmalloc_canary_size); + memset(canary2, debugmalloc_canary_char, debugmalloc_canary_size); + memset(user_mem, zero ? 0 : debugmalloc_canary_char, elem->size); +} + +/* check canary, return true if ok, false if corrupted. */ +static bool debugmalloc_canary_ok(DebugmallocElem const *elem) { + unsigned char *real_mem = (unsigned char *) elem->real_mem; + unsigned char *canary1 = real_mem; + unsigned char *canary2 = real_mem + debugmalloc_canary_size + elem->size; + for (size_t i = 0; i < debugmalloc_canary_size; ++i) { + if (canary1[i] != debugmalloc_canary_char) + return false; + if (canary2[i] != debugmalloc_canary_char) + return false; + } + return true; +} + +/* dump memory contents to log file. */ +static void debugmalloc_dump_memory(char const *mem, size_t size) { + for (unsigned y = 0; y < (size + 15) / 16; y++) { + char line[80]; + int pos = 0; + pos += sprintf(line + pos, " %04x ", y * 16); + for (unsigned x = 0; x < 16; x++) { + if (y * 16 + x < size) + pos += sprintf(line + pos, "%02x ", mem[y * 16 + x]); + else + pos += sprintf(line + pos, " "); + } + pos += sprintf(line + pos, " "); + for (unsigned x = 0; x < 16; x++) { + if (y * 16 + x < size) { + unsigned char c = mem[y * 16 + x]; + pos += sprintf(line + pos, "%c", isprint(c) ? c : '.'); + } + else { + pos += sprintf(line + pos, " "); + } + } + debugmalloc_log("%s\n", line); + } +} + +/* dump data of allocated memory block. + * if the canary is corrupted, it is also written to the log. */ +static void debugmalloc_dump_elem(DebugmallocElem const *elem) { + bool canary_ok = debugmalloc_canary_ok(elem); + + debugmalloc_log(" MEMORIATERULET: %p, %u bajt, kanari: %s\n" + " foglalas: %s:%u, %s(%s)\n", + elem->user_mem, (unsigned) elem->size, canary_ok ? "ok" : "**SERULT**", + elem->file, elem->line, + elem->func, elem->expr); + + if (!canary_ok) { + debugmalloc_log(" ELOTTE kanari: \n"); + debugmalloc_dump_memory((char const *) elem->real_mem, debugmalloc_canary_size); + } + + debugmalloc_log(" memoria eleje: \n"); + debugmalloc_dump_memory((char const *) elem->user_mem, elem->size > 64 ? 64 : elem->size); + + if (!canary_ok) { + debugmalloc_log(" UTANA kanari: \n"); + debugmalloc_dump_memory((char const *) elem->real_mem + debugmalloc_canary_size + elem->size, debugmalloc_canary_size); + } +} + + +/* dump data of all memory blocks allocated. */ +static void debugmalloc_dump(void) { + DebugmallocData *instance = debugmalloc_singleton(); + debugmalloc_log("** DEBUGMALLOC DUMP ************************************\n"); + for (size_t i = 0; i < debugmalloc_tablesize; i++) { + DebugmallocElem *head = &instance->head[i]; + for (DebugmallocElem *iter = head->next; iter->next != NULL; iter = iter->next) + debugmalloc_dump_elem(iter); + } + debugmalloc_log("** DEBUGMALLOC DUMP VEGE *******************************\n"); +} + + +/* called at program exit to dump data if there is a leak, + * ie. allocated block remained. */ +static void debugmalloc_atexit_dump(void) { + DebugmallocData *instance = debugmalloc_singleton(); + bool ok = true; + for (size_t i = 0; i < debugmalloc_tablesize; i++) { + if (instance->head[i].next != &instance->tail[i]) { + ok = false; + break; + } + } + + if (!ok) { + debugmalloc_log("********************************************************\n" + "* MEMORIASZIVARGAS VAN A PROGRAMBAN!!!\n" + "********************************************************\n"); + debugmalloc_dump(); + } +} + + +/* hash function for bucket hash. */ +static size_t debugmalloc_hash(void *address) { + /* the last few bits are ignored, as they are usually zero, because of + * alignment purposes. all tested architectures used 16 byte allocation. */ + size_t cut = (size_t)address >> 4; + return cut % debugmalloc_tablesize; +} + + +/* insert element to hash table. */ +static void debugmalloc_insert(DebugmallocElem *entry) { + DebugmallocData *instance = debugmalloc_singleton(); + size_t idx = debugmalloc_hash(entry->user_mem); + DebugmallocElem *head = &instance->head[idx]; + entry->prev = head; + entry->next = head->next; + head->next->prev = entry; + head->next = entry; +} + + +/* find element in hash table, given with the memory address that the user sees. + * @return the linked list entry, or null if not found. */ +static DebugmallocElem * debugmalloc_find(void *mem) { + DebugmallocData *instance = debugmalloc_singleton(); + size_t idx = debugmalloc_hash(mem); + DebugmallocElem *head = &instance->head[idx]; + for (DebugmallocElem *iter = head->next; iter->next != NULL; iter = iter->next) + if (iter->user_mem == mem) + return iter; + return NULL; +} + + +/* count number of blocks allocated */ +static int debugmalloc_allocated_count(void) { + DebugmallocData *instance = debugmalloc_singleton(); + int count = 0; + for (size_t i = 0; i < debugmalloc_tablesize; i++) { + for (DebugmallocElem *iter = instance->head[i].next; iter != &instance->tail[i]; iter = iter->next) + count += 1; + } + return count; +} + + +/* allocate memory. this function is called via the macro. */ +static void *debugmalloc_malloc_full(size_t size, char const *func, char const *expr, char const *file, unsigned line, bool zero) { + /* imitate standard malloc: return null if size is zero */ + if (size == 0) + return NULL; + + /* allocate more memory, make room for canary */ + void *real_mem = malloc(size + 2 * debugmalloc_canary_size); + if (real_mem == NULL) { + debugmalloc_log("debugmalloc: %s @ %s:%u: nem sikerult %u meretu memoriat foglalni!\n", func, file, line, (unsigned) size); + return NULL; + } + + /* allocate memory for linked list element */ + DebugmallocElem *newentry = (DebugmallocElem *) malloc(sizeof(DebugmallocElem)); + if (newentry == NULL) { + free(real_mem); + debugmalloc_log("debugmalloc: %s @ %s:%u: le tudtam foglalni %u memoriat, de utana a sajatnak nem, sry\n", func, file, line, (unsigned) size); + abort(); + } + + /* metadata of allocation: caller function, code line etc. */ + debugmalloc_strlcpy(newentry->func, func, sizeof(newentry->func)); + debugmalloc_strlcpy(newentry->expr, expr, sizeof(newentry->expr)); + debugmalloc_strlcpy(newentry->file, file, sizeof(newentry->file)); + newentry->line = line; + + /* address of allocated memory chunk */ + newentry->real_mem = real_mem; + newentry->user_mem = (unsigned char *) real_mem + debugmalloc_canary_size; + newentry->size = size; + debugmalloc_memory_init(newentry, zero); + + /* store in list and return pointer to user area */ + debugmalloc_insert(newentry); + return newentry->user_mem; +} + + +/* free memory and remove list item. before deleting, the chuck is filled with + * the canary byte to make sure that the user will see garbage if the memory + * is accessed after freeing. */ +static void debugmalloc_free_inner(DebugmallocElem *deleted) { + /* fill with garbage */ + memset(deleted->real_mem, debugmalloc_canary_char, deleted->size + 2 * debugmalloc_canary_size); + + /* free memory and remove from linked list */ + free(deleted->real_mem); + deleted->next->prev = deleted->prev; + deleted->prev->next = deleted->next; + free(deleted); +} + + +/* free memory - called via the macro. + * as all allocations are tracked in the list, this function can call abort() + * if a block is freed twice or the free function is called with an invalid address. */ +static void debugmalloc_free_full(void *mem, char const *func, char const *file, unsigned line) { + /* imitate standard free function: if ptr is null, no operation is performed */ + if (mem == NULL) + return; + + /* find allocation, abort if not found */ + DebugmallocElem *deleted = debugmalloc_find(mem); + if (deleted == NULL) { + debugmalloc_log("debugmalloc: %s @ %s:%u: olyan teruletet akarsz felszabaditani, ami nincs lefoglalva!\n", func, file, line); + abort(); + } + + /* check canary and then free memory */ + if (!debugmalloc_canary_ok(deleted)) { + debugmalloc_log("debugmalloc: %s @ %s:%u: a %p memoriateruletet tulindexelted!\n", func, file, line, mem); + debugmalloc_dump_elem(deleted); + } + debugmalloc_free_inner(deleted); +} + + +/* realloc-like function. */ +static void *debugmalloc_realloc_full(void *oldmem, size_t newsize, char const *func, char const *expr, char const *file, unsigned line) { + /* imitate standard realloc: equivalent to malloc if first param is NULL */ + if (oldmem == NULL) + return debugmalloc_malloc_full(newsize, func, expr, file, line, 0); + + /* imitate standard realloc: equivalent to free if size is null. */ + if (newsize == 0) { + debugmalloc_free_full(oldmem, func, file, line); + return NULL; + } + + /* find old allocation. abort if not found. */ + DebugmallocElem *entry = debugmalloc_find(oldmem); + if (entry == NULL) { + debugmalloc_log("debugmalloc: %s @ %s:%u: olyan teruletet akarsz atmeretezni, ami nincs lefoglalva!\n", func, file, line); + abort(); + } + + /* create new allocation */ + void *newmem = debugmalloc_malloc_full(newsize, func, expr, file, line, false); + if (newmem == NULL) { + debugmalloc_log("debugmalloc: %s @ %s:%u: nem sikerult uj memoriat foglalni az atmeretezeshez!\n", func, file, line); + /* imitate standard realloc: original block is untouched, but return NULL */ + return NULL; + } + + /* copy old data */ + size_t smaller = entry->size < newsize ? entry->size : newsize; + memcpy(newmem, oldmem, smaller); + debugmalloc_free_inner(entry); + return newmem; +} + + +/* initialize debugmalloc singleton. returns the newly allocated instance */ +static DebugmallocData * debugmalloc_create(void) { + /* config check */ + if (debugmalloc_canary_size % 16 != 0) { + debugmalloc_log("debugmalloc: a kanari merete legyen 16-tal oszthato\n"); + abort(); + } + if (debugmalloc_canary_char == 0) { + debugmalloc_log("debugmalloc: a kanari legyen 0-tol kulonbozo\n"); + abort(); + } + /* avoid compiler warning if these functions are not used */ + (void) debugmalloc_realloc_full; + (void) debugmalloc_log_file; + (void) debugmalloc_allocated_count; + + /* create and initialize instance */ + DebugmallocData *instance = (DebugmallocData *) malloc(sizeof(DebugmallocData)); + if (instance == NULL) { + debugmalloc_log("debugmalloc: nem sikerult elinditani a memoriakezelest\n"); + abort(); + } + memset(instance, 0, sizeof(DebugmallocData)); + for (size_t i = 0; i < debugmalloc_tablesize; i++) { + instance->head[i].prev = NULL; + instance->head[i].next = &instance->tail[i]; + instance->tail[i].next = NULL; + instance->tail[i].prev = &instance->head[i]; + } + + atexit(debugmalloc_atexit_dump); + return instance; +} + +#endif diff --git a/debugmalloc.h b/debugmalloc.h new file mode 100644 index 0000000..ec40646 --- /dev/null +++ b/debugmalloc.h @@ -0,0 +1,26 @@ +#ifndef DEBUGMALLOC_H +#define DEBUGMALLOC_H + +#include +#include "debugmalloc-impl.h" + +/* These macro-like functions forward all allocation/free + * calls to debugmalloc. Usage is the same, malloc(size) + * gives the address of a new memory block, free(ptr) + * deallocates etc. + * + * If you use this file, make sure that you include this + * in *ALL* translation units (*.c) of your source. The + * builtin free() function cannot deallocate a memory block + * that was allocated via debugmalloc, yet the name of + * the functions are the same! + * + * If there is a conflict with the macro names used here, + * you can include debugmalloc-nomacro.h instead. */ + +#define malloc(S) debugmalloc_malloc_full((S), "malloc", #S, __FILE__, __LINE__, false) +#define calloc(N,S) debugmalloc_malloc_full((N)*(S), "calloc", #N ", " #S, __FILE__, __LINE__, true) +#define realloc(P,S) debugmalloc_realloc_full((P), (S), "realloc", #S, __FILE__, __LINE__) +#define free(P) debugmalloc_free_full((P), "free", __FILE__, __LINE__) + +#endif diff --git a/main.c b/main.c index 5bb5761..8f24c76 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,19 @@ #include #define SDL_MAIN_HANDLED + +#include "debugmalloc.h" + #include +#include #include #include "Graphics.h" int main(int argc, char **argv) { + //debugmalloc_log_file("log.txt"); + SDL_Init(SDL_INIT_EVERYTHING); + TTF_Init(); SDL_Window *window = SDL_CreateWindow("Test window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -16,7 +23,7 @@ int main(int argc, char **argv) { SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - + TTF_Font *font = TTF_OpenFont("res/SourceCodePro-Regular.ttf", 25); bool quit = false; SDL_Event e; @@ -37,6 +44,7 @@ int main(int argc, char **argv) { SDL_Surface *surf = SDL_CreateRGBSurfaceWithFormat(0, 640, 480, 32, SDL_PIXELFORMAT_ABGR32); gfx_draw_bezier_cubic(surf, (Point){200, 100}, (Point){600, 400}, (Point){x, y}, (Point){300, 400}, 5, 0x0000ffff); + gfx_draw_text(surf, font, 100, 100, "Hello World!", 0xff00ffff); SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surf); SDL_FreeSurface(surf); SDL_RenderCopy(renderer, tex, NULL, NULL); @@ -46,7 +54,10 @@ int main(int argc, char **argv) { } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); + TTF_Quit(); SDL_Quit(); + malloc(sizeof(int) * 20); + return 0; } \ No newline at end of file diff --git a/res/SourceCodePro-Regular.ttf b/res/SourceCodePro-Regular.ttf new file mode 100644 index 0000000..437f472 Binary files /dev/null and b/res/SourceCodePro-Regular.ttf differ