diff --git a/Makefile b/Makefile index 3d53552..5d10ccd 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,4 @@ args = -o all: $(compiler) $(warningLevel) $(includes) $(sources) $(linker) $(libs) $(args) $(dir)\$(target) - start cmd /K "cd $(dir) && $(target)" + start cmd /C "cd $(dir) && $(target)" diff --git a/bin/assets/fonts/ae-bi.ttf b/bin/assets/fonts/ae-bi.ttf new file mode 100644 index 0000000..017f3f6 Binary files /dev/null and b/bin/assets/fonts/ae-bi.ttf differ diff --git a/bin/assets/fonts/ka1.ttf b/bin/assets/fonts/ka1.ttf new file mode 100644 index 0000000..d1df852 Binary files /dev/null and b/bin/assets/fonts/ka1.ttf differ diff --git a/bin/assets/fonts/minecraft.ttf b/bin/assets/fonts/minecraft.ttf new file mode 100644 index 0000000..85c1472 Binary files /dev/null and b/bin/assets/fonts/minecraft.ttf differ diff --git a/main.c b/main.c index e286a30..ba4d7f2 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ void DrawFrame(); void INITIALIZE(); void QUIT(); void GAMELOOP(); +void printFontStyle(TTF_Font * ffont); void mousePress(SDL_MouseButtonEvent b); void keyPress(SDL_KeyboardEvent b); void windowChanged(SDL_WindowEvent b); @@ -31,6 +32,7 @@ SDL_Window * window; SDL_Renderer * renderer; SDL_Event event; bool running = true, fullscreen = false; +TTF_Font * font = NULL; Ball ball; @@ -106,16 +108,61 @@ void DrawFrame(){ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); BREAKOUT_Draw(renderer); + + int w, h; + if (TTF_SizeText(font, "put your text here", &w, &h)) { + // perhaps print the current TTF_GetError(), the string can't be rendered... + } else { + printf("width=%d height=%d\n", w, h); + } + SDL_Color White = { 255, 255, 255 }; // this is the color in rgb format, maxing out all would give you the color white, and it will be your text's color + + SDL_Surface * surfaceMessage = TTF_RenderText_Solid(font, "put your text here", White); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first + + SDL_Texture * Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); // now you can convert it into a texture + + SDL_Rect Message_rect = (SDL_Rect) {.x = 0, .y = 0, .w = w, .h = h }; // create a rect + SDL_RenderCopy(renderer, Message, NULL, &Message_rect); // you put the renderer's name first, the Message, the crop size(you can ignore this if you don't want to dabble with cropping), and the rect which is the size and coordinate of your texture + + SDL_FreeSurface(surfaceMessage); + SDL_DestroyTexture(Message); SDL_RenderPresent(renderer); +} /* DrawFrame */ + +void printFontStyle(TTF_Font * ffont){ + int style; + + style = TTF_GetFontStyle(ffont); + printf("The font style is:"); + if (style == TTF_STYLE_NORMAL) + printf(" normal"); + else { + if (style & TTF_STYLE_BOLD) + printf(" bold"); + if (style & TTF_STYLE_ITALIC) + printf(" italic"); + if (style & TTF_STYLE_UNDERLINE) + printf(" underline"); + if (style & TTF_STYLE_STRIKETHROUGH) + printf(" strikethrough"); + } + printf("\n"); } void INITIALIZE() { srand(time(NULL)); - if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError()); else printf("SDL was successfully initialized!\n"); - if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! IMG_Error: %s\n", IMG_GetError()); + if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! Error: %s\n", IMG_GetError()); else printf("IMG was successfully initialized!\n"); - TTF_Init(); + if (TTF_Init() == -1) printf("TTF could not initialize! Error: %s\n", TTF_GetError()); + else printf("TTF was successfully initialized!\n"); + +// load font.ttf at size 16 into font + font = TTF_OpenFont("assets/fonts/minecraft.ttf", 32); + if (!font) printf("Font could not initialize! Error: %s\n", TTF_GetError()); + else printf("Font was successfully initialized!\n"); + printFontStyle(font); window = SDL_CreateWindow("BreakING", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL); SDL_SetWindowResizable(window, true); @@ -129,6 +176,8 @@ void QUIT(){ printf("De-initializing started...\n"); BREAKOUT_DEINITIALIZE(); free(keystate); + TTF_CloseFont(font); + font = NULL; // to be safe... TTF_Quit(); IMG_Quit(); printf("Quitting SDL_IMG finished!\n");