Moved rendering the titlescreen to it's own function.

This commit is contained in:
Barra Ó Catháin 2023-03-22 17:25:04 +00:00
parent 0ac4fd0989
commit 3baa01f9cb
3 changed files with 151 additions and 48 deletions

View File

@ -125,62 +125,75 @@ int main(int argc, char ** argv)
SDL_Rect textDestination = {0, 0, text->w, text->h}; SDL_Rect textDestination = {0, 0, text->w, text->h};
SDL_Texture * textTexture = SDL_CreateTextureFromSurface(renderer, text); 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; int scrollX = 0, titleAlpha = 0, textAlpha = 0;
// Render the title text: // Render the title text:
while(!inputSelected) while(!inputSelected)
{ {
// Draw the title screen: /* // Draw the title screen: */
SDL_GetWindowSize(window, &width, &height); /* SDL_GetWindowSize(window, &width, &height); */
titleRect.x = (width/2) - (317/2); /* titleRect.x = (width/2) - (317/2); */
titleRect.y = (height/2) - 51; /* titleRect.y = (height/2) - 51; */
textDestination.x = (width/2) - (textDestination.w / 2); /* textDestination.x = (width/2) - (textDestination.w / 2); */
textDestination.y = (height/2) + (textDestination.h) * 2; /* textDestination.y = (height/2) + (textDestination.h) * 2; */
// Set the colour to black: /* // Set the colour to black: */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); */
// Clear the screen, filling it with black: /* // Clear the screen, filling it with black: */
SDL_RenderClear(renderer); /* SDL_RenderClear(renderer); */
starfieldRect.x = 0 - scrollX++; /* starfieldRect.x = 0 - scrollX++; */
starfieldRect.y = 0; /* starfieldRect.y = 0; */
while(starfieldRect.x <= (width + 800)) /* while(starfieldRect.x <= (width + 800)) */
{ /* { */
while(starfieldRect.y <= (height + 800)) /* while(starfieldRect.y <= (height + 800)) */
{ /* { */
SDL_RenderCopy(renderer, starfieldTexture, NULL, &starfieldRect); /* SDL_RenderCopy(renderer, starfieldTexture, NULL, &starfieldRect); */
starfieldRect.y += 800; /* starfieldRect.y += 800; */
} /* } */
starfieldRect.y = 0; /* starfieldRect.y = 0; */
starfieldRect.x += 800; /* starfieldRect.x += 800; */
} /* } */
if(scrollX == 801) /* if(scrollX == 801) */
{ /* { */
scrollX = 0; /* scrollX = 0; */
} /* } */
SDL_SetTextureAlphaMod(titleTexture, titleAlpha); /* SDL_SetTextureAlphaMod(titleTexture, titleAlpha); */
SDL_RenderCopy(renderer, titleTexture, NULL, &titleRect); /* SDL_RenderCopy(renderer, titleTexture, NULL, &titleRect); */
if(titleAlpha < 254) /* if(titleAlpha < 254) */
{ /* { */
titleAlpha += 10; /* titleAlpha += 10; */
} /* } */
if(titleAlpha >= 254) /* if(titleAlpha >= 254) */
{ /* { */
titleAlpha = 254; /* titleAlpha = 254; */
} /* } */
SDL_SetTextureAlphaMod(textTexture, textAlpha); /* SDL_SetTextureAlphaMod(textTexture, textAlpha); */
SDL_RenderCopy(renderer, textTexture, NULL, &textDestination); /* SDL_RenderCopy(renderer, textTexture, NULL, &textDestination); */
if(textAlpha < 254 && titleAlpha == 254) /* if(textAlpha < 254 && titleAlpha == 254) */
{ /* { */
textAlpha += 20; /* textAlpha += 20; */
} /* } */
if(textAlpha >= 254) /* if(textAlpha >= 254) */
{ /* { */
textAlpha = 254; /* textAlpha = 254; */
} /* } */
SDL_RenderPresent(renderer);
drawTitleScreen(&titlescreen);
SDL_PumpEvents(); SDL_PumpEvents();
SDL_GetKeyboardState(&keyCount); SDL_GetKeyboardState(&keyCount);
@ -434,5 +447,5 @@ int main(int argc, char ** argv)
} }
// ======================================================================================================== // ========================================================================================================
// Local Variables: // Local Variables:
// compile-command: "gcc `sdl2-config --libs --cflags` Spacewar.c spacewarPlayer.c -lSDL2_image -lm -o 'Spacewar!'" // compile-command: "gcc `sdl2-config --libs --cflags` Spacewar.c spacewarPlayer.c spacewarGraphics.c -lSDL2_image -lSDL2_ttf -lm -o 'Spacewar!'"
// End: // End:

79
spacewarGraphics.c Normal file
View File

@ -0,0 +1,79 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_ttf.h>
#include <stdint.h>
#include "spacewarGraphics.h"
void drawTitleScreen(SpacewarTitlescreen * titlescreen)
{
// Get the current size of the window:
int width = 0, height = 0;
SDL_GetWindowSize(titlescreen->window, &width, &height);
// Position the elements on-screen:
titlescreen->titleRectangle->x = (width/2) - (titlescreen->titleRectangle->w / 2);
titlescreen->titleRectangle->y = (height/2) - titlescreen->titleRectangle->h;
titlescreen->textRectangle->x = (width/2) - (titlescreen->textRectangle->w / 2);
titlescreen->textRectangle->y = (height/2) + (titlescreen->textRectangle->h * 2);
// Set the renderer colour to black and clear the screen:
SDL_SetRenderDrawColor(titlescreen->renderer, 0, 0, 0, 255);
SDL_RenderClear(titlescreen->renderer);
// Set the correct position to begin the starfield, and scroll it back for the next frame:
titlescreen->starfieldRectangle->x = 0 - titlescreen->xScroll++;
titlescreen->starfieldRectangle->y = 0;
// Draw the starfield by tiling the starfield texture:
while (titlescreen->starfieldRectangle->x <= (width + titlescreen->starfieldRectangle->w))
{
// Go down, covering a column of the screen:
while(titlescreen->starfieldRectangle->y <= (height + titlescreen->starfieldRectangle->h))
{
SDL_RenderCopy(titlescreen->renderer, titlescreen->starfieldTexture, NULL,
titlescreen->starfieldRectangle);
titlescreen->starfieldRectangle->y += titlescreen->starfieldRectangle->h;
}
// Back to the top, move over one texture width:
titlescreen->starfieldRectangle->y = 0;
titlescreen->starfieldRectangle->x += titlescreen->starfieldRectangle->w;
}
// Reset the xScroll if it goes farther than a texture width away:
if (titlescreen->xScroll == titlescreen->starfieldRectangle->w + 1)
{
titlescreen->xScroll = 0;
}
// Set the opacity of the logo so we can fade it in:
if (titlescreen->titleAlpha < 254)
{
titlescreen->titleAlpha += 10;
}
if (titlescreen->titleAlpha >= 254)
{
titlescreen->titleAlpha = 254;
}
SDL_SetTextureAlphaMod(titlescreen->titleTexture, titlescreen->titleAlpha);
// Set the opacity of the text so we can fade it in after we fade in the logo:
if (titlescreen->textAlpha < 254 && titlescreen->titleAlpha == 254)
{
titlescreen->textAlpha += 10;
}
if (titlescreen->textAlpha >= 254)
{
titlescreen->textAlpha = 254;
}
SDL_SetTextureAlphaMod(titlescreen->textTexture, titlescreen->textAlpha);
// Display the logo and text:
SDL_RenderCopy(titlescreen->renderer, titlescreen->titleTexture, NULL, titlescreen->titleRectangle);
SDL_RenderCopy(titlescreen->renderer, titlescreen->textTexture, NULL, titlescreen->textRectangle);
// Display to the renderer:
SDL_RenderPresent(titlescreen->renderer);
}

View File

@ -2,6 +2,17 @@
#define SPACEWAR_GRAPHICS_H #define SPACEWAR_GRAPHICS_H
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
typedef struct SpacewarTitlescreen
{
SDL_Window * window;
SDL_Renderer * renderer;
uint16_t xScroll, titleAlpha, textAlpha;
SDL_Texture * titleTexture, * textTexture, * starfieldTexture;
SDL_Rect * titleRectangle, * textRectangle, * starfieldRectangle;
} SpacewarTitlescreen;
void drawTitleScreen(SpacewarTitlescreen * titlescreen);
static inline void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius) static inline void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{ {
const int32_t diameter = (radius * 2); const int32_t diameter = (radius * 2);