From 2cca8be0df33606f0ebf15bfa389e922a96eee88 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Sat, 25 Mar 2023 15:25:32 +0000 Subject: [PATCH] Made the titlescreens use prepareTitleScreen() --- Spacewar.c | 37 +++++++------------------------------ spacewarGraphics.c | 5 +++++ 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/Spacewar.c b/Spacewar.c index 39efedb..6969b88 100644 --- a/Spacewar.c +++ b/Spacewar.c @@ -120,23 +120,11 @@ int main(int argc, char ** argv) { playerOne.joystick = NULL; bool inputSelected = false; - SDL_Surface * text = TTF_RenderText_Blended(font, "Press Enter to play.", white); - SDL_Rect textDestination = {0, 0, text->w, text->h}; - SDL_Texture * textTexture = SDL_CreateTextureFromSurface(renderer, text); - - // Prep the titlescreen struct: - SpacewarTitlescreen titlescreen; - titlescreen.starfieldTexture = starfieldTexture; - titlescreen.starfieldRectangle = &starfieldRect; - titlescreen.xScroll = 0; - titlescreen.window = window; - titlescreen.renderer = renderer; - titlescreen.titleRectangle = &titleRect; - titlescreen.titleTexture = titleTexture; - titlescreen.textTexture = textTexture; - titlescreen.textRectangle = &textDestination; - int scrollX = 0, titleAlpha = 0, textAlpha = 0; + // Prep the titlescreen struct: + SpacewarTitlescreen titlescreen = prepareTitleScreen(window, renderer, "./Images/Starfield.png", + "./Images/Title.png", font, "Press Enter to play."); + // Render the title text: while(!inputSelected) { @@ -157,21 +145,10 @@ int main(int argc, char ** argv) } else { - SDL_Surface * text = TTF_RenderText_Blended(font, "Press Button 0 on your controller, or press enter.", white); - SDL_Rect textDestination = {0, 0, text->w, text->h}; - SDL_Texture * textTexture = SDL_CreateTextureFromSurface(renderer, text); - // Prep the titlescreen struct: - SpacewarTitlescreen titlescreen; - titlescreen.starfieldTexture = starfieldTexture; - titlescreen.starfieldRectangle = &starfieldRect; - titlescreen.xScroll = 0; - titlescreen.window = window; - titlescreen.renderer = renderer; - titlescreen.titleRectangle = &titleRect; - titlescreen.titleTexture = titleTexture; - titlescreen.textTexture = textTexture; - titlescreen.textRectangle = &textDestination; + SpacewarTitlescreen titlescreen = prepareTitleScreen(window, renderer, "./Images/Starfield.png", + "./Images/Title.png", font, + "Press Enter or Button 0 on your joystick to play."); // Load all joysticks: int joystickListLength = SDL_NumJoysticks(); diff --git a/spacewarGraphics.c b/spacewarGraphics.c index 7b9e450..31367d0 100644 --- a/spacewarGraphics.c +++ b/spacewarGraphics.c @@ -35,18 +35,23 @@ SpacewarTitlescreen prepareTitleScreen(SDL_Window * window, SDL_Renderer * rende newTitleScreen.titleRectangle = calloc(1, sizeof(SDL_Rect)); newTitleScreen.textRectangle = calloc(1, sizeof(SDL_Rect)); newTitleScreen.starfieldRectangle = calloc(1, sizeof(SDL_Rect)); + + // Set the rects to the size of the textures: SDL_QueryTexture(newTitleScreen.textTexture, NULL, NULL, NULL, &newTitleScreen.textRectangle->h); SDL_QueryTexture(newTitleScreen.textTexture, NULL, NULL, &newTitleScreen.textRectangle->w, NULL); SDL_QueryTexture(newTitleScreen.titleTexture, NULL, NULL, NULL, &newTitleScreen.titleRectangle->h); SDL_QueryTexture(newTitleScreen.titleTexture, NULL, NULL, &newTitleScreen.titleRectangle->w, NULL); SDL_QueryTexture(newTitleScreen.starfieldTexture, NULL, NULL, NULL, &newTitleScreen.starfieldRectangle->h); SDL_QueryTexture(newTitleScreen.starfieldTexture, NULL, NULL, &newTitleScreen.starfieldRectangle->w, NULL); + + return newTitleScreen; } void drawTitleScreen(SpacewarTitlescreen * titlescreen) { // Get the current size of the window: int width = 0, height = 0; + printf("%p\n", titlescreen->window); SDL_GetWindowSize(titlescreen->window, &width, &height); // Position the elements on-screen: