diff --git a/src/font.c b/src/font.c new file mode 100644 index 0000000..8061a9c --- /dev/null +++ b/src/font.c @@ -0,0 +1,81 @@ +#include "font.h" + +int createGrafixFont(grafixFont *font, char* directory ,int fontSize){ + + FT_Library library; + if (FT_Init_FreeType(&library)) { + setGrafixError("grafixError:Unable To Load Library;reason:Can't Init FreeType;"); + return 0; + } + + if (FT_New_Face(library, directory, 0, &font->face)) { + setGrafixError("grafixError:Unable To Load Face;reason:unknow;"); + return 0; + } + + font->fontSize = fontSize; + font->directory = directory; + font->slot = font->face->glyph; + + return 1; +} + + +void drawGrafixText(grafixWindow window, grafixFont font, int x, int y, char* text, grafixColor color){ + if( WINDOWS[window.id] == NULL|| window.isDead ) return; + + if (FT_Set_Pixel_Sizes(font.face, 0, font.fontSize)) { + setGrafixError("grafixError:Unable To Set FontSize;reason:unknow;"); + return; + } + + int width = 0, height = 0; + FT_GlyphSlot slot = font.face->glyph; + FT_Vector pen = {0, 0}; + + int text_length = strlen(text); + for (int i = 0; i < text_length; ++i) { + if (FT_Load_Char(font.face, text[i], FT_LOAD_RENDER)) { + continue; + } + + pen.x += slot->advance.x >> 6; + pen.y += slot->advance.y >> 6; + + if (pen.x > width) { + width = pen.x; + } + if (pen.y > height) { + height = pen.y; + } + } + + unsigned char* buffer = (unsigned char*)malloc(width * height * sizeof(unsigned char)); + if (!buffer) { + setGrafixError("grafixError:Unable To Create Buffer;reason:No Memory Available For Buffer;"); + return; + } + memset(buffer, 0, width * height * sizeof(unsigned char)); + + pen.x = 0; + pen.y = 0; + for (int i = 0; i < text_length; ++i) { + if (FT_Load_Char(font.face, text[i], FT_LOAD_RENDER)) { + continue; + } + + FT_Bitmap bitmap = slot->bitmap; + for (int _y = 0; _y < bitmap.rows; ++_y) { + for (int _x = 0; _x < bitmap.width; ++_x) { + _setPixel(window,x+_x+pen.x,y+_y+pen.y,(grafixColor){bitmap.buffer[y * bitmap.width + x],0,0}); + // BUFFERS[window.id].frameBuffer[(pen.y + y) * width + pen.x + x] = bitmap.buffer[(y * bitmap.width + x) ]; + // BUFFERS[window.id].frameBuffer[((pen.y + y) * width + pen.x + x)+1] = bitmap.buffer[(y * bitmap.width + x)+1 ]; + // BUFFERS[window.id].frameBuffer[((pen.y + y) * width + pen.x + x)+2] = bitmap.buffer[(y * bitmap.width + x)+2 ]; + } + } + + pen.x += slot->advance.x >> 6; + pen.y += slot->advance.y >> 6; + } + +} diff --git a/src/font.h b/src/font.h new file mode 100644 index 0000000..5145908 --- /dev/null +++ b/src/font.h @@ -0,0 +1,17 @@ +#ifndef FONT_H +#define FONT_H + +#include "grafix.h" + +typedef struct FONT{ + char* directory; + int fontSize; + FT_Face face; + FT_GlyphSlot slot; +}grafixFont; + +int createGrafixFont(grafixFont *font, char* directory ,int fontSize); +void drawGrafixText(grafixWindow window, grafixFont font, int x, int y, char* text, grafixColor color); + + +#endif /* FONT_H */ \ No newline at end of file diff --git a/src/grafix.c b/src/grafix.c index 24bca47..d8531a3 100644 --- a/src/grafix.c +++ b/src/grafix.c @@ -155,9 +155,6 @@ int createGrafixWindow(grafixWindow* window, int WIDTH, int HEIGHT, char* NAME){ time._waitCounter = 0; time._startCounter = clock(); - // QueryPerformanceCounter(&TIMES[time.id]._startCounter); - // QueryPerformanceFrequency(&TIMES[time.id]._frequency); - TIMES[time.id] = time; } diff --git a/src/grafix.h b/src/grafix.h index 6c8f633..dfda24e 100644 --- a/src/grafix.h +++ b/src/grafix.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include FT_FREETYPE_H #endif /* _DEPENDENCIES_H_ */ @@ -18,6 +20,7 @@ #include "event.h" #include "time.h" #include "image.h" +#include "font.h" #endif /* _GRAFIX_DEPENDENCIES_H_ */ diff --git a/src/image.c b/src/image.c index 89258cf..81c6838 100644 --- a/src/image.c +++ b/src/image.c @@ -1,17 +1,12 @@ #include "image.h" -static int IMAGE_ID; -grafixImage _IMAGES[MAX_IMAGE]; - int createGrafixImage(grafixImage *image, const char* filePath, int fileType){ image->_isInit = 0; - image->id = IMAGE_ID++; image->fileType = fileType; FILE *image_file; image_file = fopen(filePath, "rb"); if (image_file == NULL){ - IMAGE_ID--; setGrafixError("grafixError:Unable To Load Image;Reason:No File Found"); return 0; } @@ -27,7 +22,6 @@ int createGrafixImage(grafixImage *image, const char* filePath, int fileType){ image->_frame = (unsigned char*) malloc(image->width * image->height * 3); if(image->_frame == NULL){ - IMAGE_ID--; fclose(image_file); setGrafixError("grafixError:Unable To Load Image;Reason:No Memory Available"); return 0; diff --git a/src/image.h b/src/image.h index 8bd0756..7178a31 100644 --- a/src/image.h +++ b/src/image.h @@ -11,7 +11,6 @@ #define FT_JPG 2 typedef struct IMAGE { - int id; char* filePath; int fileType; @@ -27,8 +26,6 @@ typedef struct IMAGE { } grafixImage; -static int IMAGE_ID; -extern grafixImage _IMAGES[MAX_IMAGE]; int createGrafixImage(grafixImage *image, const char* filePath, int fileType); void drawGrafixImage(grafixWindow window, grafixImage image, int x, int y); diff --git a/src/shapes.c b/src/shapes.c index bc02896..4c69b1b 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -129,68 +129,3 @@ void drawGrafixCircle(grafixWindow window, int xc, int yc, int r, grafixColor co } } - -void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color){ - if( WINDOWS[window.id] == NULL|| window.isDead ) return; - - HDC hdc = CreateCompatibleDC(NULL); - - HFONT hFont = CreateFont(fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,"Arial"); - SelectObject(hdc, hFont); - - SIZE size; - GetTextExtentPoint32(hdc, text, strlen(text), &size); - - RECT rc = { 0, 0, size.cx, size.cy }; - DrawText(hdc, text, -1, &rc, DT_CALCRECT); - - BITMAPINFO bmi = { 0 }; - bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bmi.bmiHeader.biWidth = size.cx; - bmi.bmiHeader.biHeight = -size.cy; - bmi.bmiHeader.biPlanes = 1; - bmi.bmiHeader.biBitCount = 24; - bmi.bmiHeader.biCompression = BI_RGB; - unsigned char* pixels = (unsigned char*)malloc(bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 3); - pixels[0]; - - HBITMAP hBitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, 0); - HGDIOBJ hOldBitmap = SelectObject(hdc, hBitmap); - - if( color.red != 0 && color.green != 0 && color.blue != 0 ){ - SetBkMode(hdc, TRANSPARENT); - }else{ - SetBkMode(hdc,OPAQUE); - SetBkColor(hdc,RGB(255,255,255)); - } - - SetTextColor(hdc, RGB(color.red, color.green, color.blue)); - DrawText(hdc, text, -1, &rc, DT_LEFT | DT_TOP); - - int index = 0;int pindex = 0; - for(int i = x; i < x+size.cx; i++){ - for(int j = y; j < y+size.cy; j++){ - if(i >= window.width || j >= window.height || i < 0 || j < 0 ) continue; - - index = (i + j * window.width) * 3; - pindex = ( (i-x) + (j-y) * size.cx) * 3; - - if( - ((color.red != 0 && color.green != 0 && color.blue != 0) && - (pixels[pindex] == 0 && pixels[pindex + 1] == 0 && pixels[pindex + 2] == 0)) || - ((color.red == 0 && color.green == 0 && color.blue == 0) && - (pixels[pindex] == 255 && pixels[pindex + 1] == 255 && pixels[pindex + 2] == 255)) - - )continue; - - BUFFERS[window.id].frameBuffer[index] = pixels[pindex]; - BUFFERS[window.id].frameBuffer[index + 1] = pixels[pindex + 1]; - BUFFERS[window.id].frameBuffer[index + 2] = pixels[pindex + 2]; - } - } - - SelectObject(hdc, hOldBitmap); - DeleteObject(hBitmap); - DeleteDC(hdc); - -} diff --git a/src/shapes.h b/src/shapes.h index 0f735a8..aa7a513 100644 --- a/src/shapes.h +++ b/src/shapes.h @@ -14,7 +14,4 @@ void drawGrafixLine(grafixWindow window, int x1, int y1, int x2, int y2, grafixC void drawGrafixRect(grafixWindow window, int x, int y, int width, int height, grafixColor color, int fillMode, int borderWidth); void drawGrafixCircle(grafixWindow window, int x, int y, int radius, grafixColor color, int fillMode, int borderWidth); -//I haven't created a seperate header file -void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color); - #endif /* SHAPES_H */ \ No newline at end of file