diff --git a/.gitignore b/.gitignore index df27c9b..28e1c94 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin* *.o -.settings* \ No newline at end of file +.settings* +build \ No newline at end of file diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 735c5ce..3265c6b 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b4f5317 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,48 @@ +# This builds a 32-bit version of library, building 64 bit is not supported yet. + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type") +endif() +set(CMAKE_BUILD_TYPE_VALUES "Debug;Release" CACHE INTERNAL "List of supported build") +set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_BUILD_TYPE_VALUES}) + +cmake_minimum_required(VERSION 3.0) + +project(glez LANGUAGES C VERSION 0.0.1) + +set(export_dest "lib/${PROJECT_NAME}-${PROJECT_VERSION}") +set(include_dest "include/${PROJECT_NAME}-${PROJECT_VERSION}") +set(lib_dest "${export_dest}/${CMAKE_BUILD_TYPE}") + +find_package(Freetype REQUIRED) +find_package(PNG REQUIRED) +find_package(GLEW REQUIRED) + +add_library(${PROJECT_NAME} SHARED "") + +set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") + +target_include_directories(${PROJECT_NAME} PRIVATE + $ + $ + $ + $ + $ +) + +target_include_directories(${PROJECT_NAME} PUBLIC + $ +) + +target_compile_definitions(${PROJECT_NAME} PRIVATE ${PNG_DEFINITIONS}) + +target_link_libraries(${PROJECT_NAME} ${FREETYPE_LIBRARIES} ${PNG_LIBRARIES} ${GLEW_LIBRARIES}) + +add_subdirectory(include) +add_subdirectory(src) +add_subdirectory(ftgl) + +install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} DESTINATION "${lib_dest}") +install(FILES "include/glez.h" DESTINATION "${include_dest}") +install(EXPORT ${PROJECT_NAME} DESTINATION "${lib_dest}") +install(FILES ${PROJECT_NAME}-config.cmake DESTINATION ${export_dest}) \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index a598fa7..0000000 --- a/Makefile +++ /dev/null @@ -1,76 +0,0 @@ -CC=$(shell sh -c "which gcc-7 || which gcc") -CFLAGS=-O3 -Wall -fPIC -fmessage-length=0 -D_GNU_SOURCE=1 -g3 -ggdb -Iinclude -isystemftgl -isystem/usr/local/include/freetype2 -isystem/usr/include/freetype2 -LDFLAGS=-shared -Wl,--no-undefined -LDLIBS=-lm -lrt -lGL -lfreetype -lGLEW -lpng -SRC_DIR=src -BIN32_DIR=bin32 -BIN64_DIR=bin64 -SOURCES=$(shell find $(SRC_DIR) -name "*.c" -print) -SOURCES+=$(shell find "ftgl" -name "*.c" -print) -OBJECTS=$(SOURCES:.c=.o) - -LIB32_PATH=/lib/i386-linux-gnu -LIB64_PATH=/lib/x86_64-linux-gnu - -TARGET32=$(BIN32_DIR)/libglez.so -TARGET64=$(BIN64_DIR)/libglez.so -TARGET=undefined - -.PHONY: clean clean_objects - -ifeq ($(ARCH),32) -CFLAGS+=-m32 -LDFLAGS+=-m32 -TARGET=$(TARGET32) -endif -ifeq ($(ARCH),64) -TARGET=$(TARGET64) -endif - -all: - mkdir -p $(BIN32_DIR) - mkdir -p $(BIN64_DIR) -ifndef ARCH - $(MAKE) clean_objects - $(MAKE) -e ARCH=32 - $(MAKE) clean_objects - $(MAKE) -e ARCH=64 -else - $(MAKE) clean_objects - $(MAKE) $(TARGET) -endif - -install: - cp $(TARGET32) $(LIB32_PATH) - cp $(TARGET64) $(LIB64_PATH) - cp -R include/. /usr/local/include/glez - -ftgl/distance-field.o : CFLAGS+=-w -ftgl/edtaa3func.o : CFLAGS+=-w -ftgl/font-manager.o : CFLAGS+=-w -ftgl/mat4.o : CFLAGS+=-w -ftgl/platform.o : CFLAGS+=-w -ftgl/shader.o : CFLAGS+=-w -ftgl/text-buffer.o : CFLAGS+=-w -ftgl/texture-atlas.o : CFLAGS+=-w -ftgl/utf8-utils.o : CFLAGS+=-w -ftgl/texture-font.o : CFLAGS+=-w -ftgl/vector.o : CFLAGS+=-w -ftgl/vertex-attribute.o : CFLAGS+=-w -ftgl/vertex-buffer.o : CFLAGS+=-w -ftgl/makefont.o : CFLAGS+=-w - -.c.o: - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGET): $(OBJECTS) - $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@ - -clean_objects: - find . -type f -name '*.o' -delete - -clean: - find . -type f -name '*.o' -delete - find . -type f -name '*.d' -delete - rm -f bin32/*.so - rm -f bin64/*.so \ No newline at end of file diff --git a/ftgl/CMakeLists.txt b/ftgl/CMakeLists.txt new file mode 100644 index 0000000..5352103 --- /dev/null +++ b/ftgl/CMakeLists.txt @@ -0,0 +1,32 @@ +target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/distance-field.h" + "${CMAKE_CURRENT_LIST_DIR}/edtaa3func.h" + "${CMAKE_CURRENT_LIST_DIR}/font-manager.h" + "${CMAKE_CURRENT_LIST_DIR}/freetype-gl.h" + "${CMAKE_CURRENT_LIST_DIR}/markup.h" + "${CMAKE_CURRENT_LIST_DIR}/mat4.h" + "${CMAKE_CURRENT_LIST_DIR}/opengl.h" + "${CMAKE_CURRENT_LIST_DIR}/platform.h" + "${CMAKE_CURRENT_LIST_DIR}/text-buffer.h" + "${CMAKE_CURRENT_LIST_DIR}/texture-atlas.h" + "${CMAKE_CURRENT_LIST_DIR}/texture-font.h" + "${CMAKE_CURRENT_LIST_DIR}/utf8-utils.h" + "${CMAKE_CURRENT_LIST_DIR}/vec234.h" + "${CMAKE_CURRENT_LIST_DIR}/vector.h" + "${CMAKE_CURRENT_LIST_DIR}/vertex-attribute.h" + "${CMAKE_CURRENT_LIST_DIR}/vertex-buffer.h") + +target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/distance-field.c" + "${CMAKE_CURRENT_LIST_DIR}/edtaa3func.c" + "${CMAKE_CURRENT_LIST_DIR}/font-manager.c" + "${CMAKE_CURRENT_LIST_DIR}/makefont.c" + "${CMAKE_CURRENT_LIST_DIR}/mat4.c" + "${CMAKE_CURRENT_LIST_DIR}/platform.c" + "${CMAKE_CURRENT_LIST_DIR}/text-buffer.c" + "${CMAKE_CURRENT_LIST_DIR}/texture-atlas.c" + "${CMAKE_CURRENT_LIST_DIR}/texture-font.c" + "${CMAKE_CURRENT_LIST_DIR}/utf8-utils.c" + "${CMAKE_CURRENT_LIST_DIR}/vector.c" + "${CMAKE_CURRENT_LIST_DIR}/vertex-attribute.c" + "${CMAKE_CURRENT_LIST_DIR}/vertex-buffer.c") \ No newline at end of file diff --git a/glez-config.cmake b/glez-config.cmake new file mode 100644 index 0000000..4dce37c --- /dev/null +++ b/glez-config.cmake @@ -0,0 +1,2 @@ +get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +include(${SELF_DIR}/${CMAKE_BUILD_TYPE}/glez.cmake) \ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..c8f6547 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/glez.h") + +add_subdirectory(internal) \ No newline at end of file diff --git a/include/glez.h b/include/glez.h index 65645b5..22184c6 100644 --- a/include/glez.h +++ b/include/glez.h @@ -98,7 +98,7 @@ void glez_rect_outline(float x, float y, float w, float h, glez_rgba_t color, void glez_rect_textured(float x, float y, float w, float h, glez_rgba_t color, glez_texture_t texture, float tx, float ty, float tw, - float th); + float th, float angle); void glez_string(float x, float y, const char *string, glez_font_t font, glez_rgba_t color, float *out_x, float *out_y); diff --git a/include/internal/CMakeLists.txt b/include/internal/CMakeLists.txt new file mode 100644 index 0000000..79ae3c8 --- /dev/null +++ b/include/internal/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/draw.h" + "${CMAKE_CURRENT_LIST_DIR}/fonts.h" + "${CMAKE_CURRENT_LIST_DIR}/program.h" + "${CMAKE_CURRENT_LIST_DIR}/textures.h") \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..bc1b40e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/draw.c" + "${CMAKE_CURRENT_LIST_DIR}/fonts.c" + "${CMAKE_CURRENT_LIST_DIR}/glez.c" + "${CMAKE_CURRENT_LIST_DIR}/program.c" + "${CMAKE_CURRENT_LIST_DIR}/textures.c") \ No newline at end of file diff --git a/src/glez.c b/src/glez.c index 64366b7..01209e0 100644 --- a/src/glez.c +++ b/src/glez.c @@ -11,8 +11,10 @@ #include "internal/draw.h" #include "internal/fonts.h" #include "internal/textures.h" +#include #include +#include /* State functions */ @@ -135,10 +137,9 @@ void glez_rect_outline(float x, float y, float w, float h, glez_rgba_t color, glez_line(x + w, y + h, -w, 0, color, thickness); glez_line(x, y + h, 0, -h, color, thickness); } - void glez_rect_textured(float x, float y, float w, float h, glez_rgba_t color, glez_texture_t texture, float tx, float ty, float tw, - float th) + float th, float angle) { internal_texture_t *tex = internal_texture_get(texture); internal_texture_bind(texture); @@ -184,6 +185,36 @@ void glez_rect_textured(float x, float y, float w, float h, glez_rgba_t color, vertices[3].color = color; vertices[3].mode = DRAW_MODE_TEXTURED; + if (angle) { + float v1[2] = {vertices[0].position.x, vertices[0].position.y}; + float v2[2] = {vertices[1].position.x, vertices[1].position.y}; + float v3[2] = {vertices[2].position.x, vertices[2].position.y}; + float v4[2] = {vertices[3].position.x, vertices[3].position.y}; + vertices[0].position.x = -tw; + vertices[1].position.x = -tw; + vertices[2].position.x = tw; + vertices[3].position.x = tw; + + vertices[0].position.y = -th; + vertices[1].position.y = th; + vertices[2].position.y = th; + vertices[3].position.y = -th; + for (int i = 0; i < 4; i++) { + float x = vertices[i].position.x; + float y = vertices[i].position.y; + vertices[i].position.x = x *cos(angle) - y *sin(angle); + vertices[i].position.y = x *sin(angle) + y *cos(angle); + } + vertices[0].position.x += v1[0]; + vertices[0].position.y += v1[1]; + vertices[1].position.x += v2[0]; + vertices[1].position.y += v2[1]; + vertices[2].position.x += v3[0]; + vertices[2].position.y += v3[1]; + vertices[3].position.x += v4[0]; + vertices[3].position.y += v4[1]; + } + vertex_buffer_push_back(program.buffer, vertices, 4, indices, 6); } diff --git a/src/textures.c b/src/textures.c index 6ea4369..8df8332 100644 --- a/src/textures.c +++ b/src/textures.c @@ -52,13 +52,14 @@ int internal_texture_load_png_rgba(const char *name, internal_texture_t *out) png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (pngstr == NULL) { + fclose(file); return -1; } png_infop pnginfo = png_create_info_struct(pngstr); png_infop pngend = png_create_info_struct(pngstr); if (setjmp(png_jmpbuf(pngstr))) { - png_destroy_read_struct(pngstr, pnginfo, pngend); + png_destroy_read_struct(&pngstr, &pnginfo, &pngend); return -1; } png_init_io(pngstr, file); @@ -71,7 +72,7 @@ int internal_texture_load_png_rgba(const char *name, internal_texture_t *out) int row_bytes; if (PNG_COLOR_TYPE_RGBA != png_get_color_type(pngstr, pnginfo)) { - png_destroy_read_struct(pngstr, pnginfo, pngend); + png_destroy_read_struct(&pngstr, &pnginfo, &pngend); fclose(file); return -1; } @@ -81,14 +82,14 @@ int internal_texture_load_png_rgba(const char *name, internal_texture_t *out) out->data = malloc(row_bytes * height * sizeof(png_byte)); if (out->data == NULL) { - png_destroy_read_struct(pngstr, pnginfo, pngend); + png_destroy_read_struct(&pngstr, &pnginfo, &pngend); fclose(file); return -1; } row_pointers = malloc(height * sizeof(png_bytep)); if (row_pointers == NULL) { - png_destroy_read_struct(pngstr, pnginfo, pngend); + png_destroy_read_struct(&pngstr, &pnginfo, &pngend); free(out->data); fclose(file); return -1;