From ee1e1d43b7c1abbeab482d2bdf767bdf0c132e4f Mon Sep 17 00:00:00 2001 From: Nathan Misner Date: Tue, 21 Jun 2022 22:47:21 -0400 Subject: [PATCH] added original logo --- intro.cpp | 1 + logo.cpp | 95 ++++++++++++++++++++++++++++++++++++--------------- menu_text.cpp | 4 +-- palette.cpp | 43 ++++++++++++++++++----- palette.h | 7 ++-- raster.cpp | 77 +++++++++++++++++++++++++++++------------ raster.h | 9 +++++ 7 files changed, 174 insertions(+), 62 deletions(-) diff --git a/intro.cpp b/intro.cpp index 1c42d42..fa52fa3 100644 --- a/intro.cpp +++ b/intro.cpp @@ -291,6 +291,7 @@ int Intro_Run() { switch(state) { case STATE_INTRO_INIT: Global_SetHiRes(); + Fade_Stop(); girlNum = 0; fontTextures = Sprite_LoadPak("FNT24x72.pak"); diff --git a/logo.cpp b/logo.cpp index 81943f4..75b9fc0 100644 --- a/logo.cpp +++ b/logo.cpp @@ -20,74 +20,113 @@ #include #include +#include "fade.h" #include "globals.h" #include "pad.h" #include "raster.h" +#include "speed.h" #include "trig.h" typedef enum { LOGO_STATE_INIT, LOGO_STATE_MESH, LOGO_STATE_FLASH, + LOGO_STATE_RING, LOGO_STATE_FADEOUT } LOGO_STATE; static std::unique_ptr logo; +static std::vector logoPal; #define LOGO_WIDTH (256) #define LOGO_HEIGHT (64) +#define LOGO_X ((WINDOW_LOWIDTH / 2) - (LOGO_WIDTH / 2)) +#define LOGO_Y ((WINDOW_LOHEIGHT / 2) - (LOGO_HEIGHT / 2)) static std::array lines; static int state = LOGO_STATE_INIT; - +// the color of the ring on the logo +#define LOGO_RED (248) +static float red; int Logo_Run() { static float amplitude = 0; static float angle = 0; static float wavelength = 0; + static float x = 0; + static float y = WINDOW_LOHEIGHT; switch (state) { case LOGO_STATE_INIT: Global_SetLoRes(); - logo = std::make_unique("oldlogo.png"); + logoPal = std::vector(); + // this is the red color from the ring on the logo + logoPal.push_back(SDL_MapRGB(gPaletteFormat, LOGO_RED, 0, 0)); + logo = std::make_unique("oldlogo.png", logoPal); + // raster lines lines = std::array(); for (int i = 0; i < lines.size(); i++) { - lines[i] = { .x = 0, .y = i }; + if (i < (LOGO_Y + LOGO_HEIGHT)) { + lines[i] = { .x = 0, .y = i }; + } + else { + // make the table for the "split up the logo" effect + int offset = i - (LOGO_Y + LOGO_HEIGHT); + x = (WINDOW_LOWIDTH / 2) * (1 - cos(DEG_TO_RAD(offset / (WINDOW_LOHEIGHT / 180)))); + if ((i % 2) == 0) { + lines[i] = { .x = (int)x, .y = i }; + } + else { + lines[i] = { .x = (int)-x, .y = i }; + } + } } + // hide the ring + logoPal[0] = SDL_MapRGB(gPaletteFormat, 0, 0, 0); + logo->updatePalette(logoPal); + red = 0; state = LOGO_STATE_MESH; break; case LOGO_STATE_MESH: - for (int i = 0; i < lines.size(); i++) { - int x = (int)(amplitude * sin(DEG_TO_RAD(i * wavelength + angle))); - if ((i % 2) == 0) { - lines[i] = {.x = x, .y = i}; + if (y > LOGO_Y) { + y -= Speed_Multiplier(); + if (y <= LOGO_Y) { + y = LOGO_Y; + Fade_InWhite(60); + state = LOGO_STATE_FLASH; } - else { - lines[i] = {.x = -x, .y = i}; - } - } - if (padData & PAD_UP) { - angle++; - } - if (padData & PAD_DOWN) { - angle--; - } - if (padData & PAD_LEFT) { - amplitude--; } - if (padData & PAD_RIGHT) { - amplitude++; - } - if (padData & PAD_DBG1) { - wavelength -= 0.01; + + logo->disp(LOGO_X, y, lines); + break; + + case LOGO_STATE_FLASH: + if (Fade_InDone()) { + state = LOGO_STATE_RING; } - if (padData & PAD_DBG2) { - wavelength += 0.01; + + logo->disp(LOGO_X, y, lines); + break; + + case LOGO_STATE_RING: + red += 2 * Speed_Multiplier(); + if (red >= LOGO_RED) { + red = LOGO_RED; + Fade_Out(120); + state = LOGO_STATE_FADEOUT; } + logoPal[0] = SDL_MapRGB(gPaletteFormat, (int)red, 0, 0); + logo->updatePalette(logoPal); + logo->disp(LOGO_X, y, lines); + break; - printf("ang: %f, amp: %f, wl: %f\n", angle, amplitude, wavelength); + case LOGO_STATE_FADEOUT: + if (Fade_OutDone()) { + state = LOGO_STATE_INIT; + return 1; + } - logo->disp((WINDOW_LOWIDTH / 2) - (LOGO_WIDTH / 2), (WINDOW_LOHEIGHT / 2) - (LOGO_HEIGHT / 2), lines); + logo->disp(LOGO_X, y, lines); break; } diff --git a/menu_text.cpp b/menu_text.cpp index fdb7630..64c3d7f 100644 --- a/menu_text.cpp +++ b/menu_text.cpp @@ -184,7 +184,7 @@ static void Menu_Text_Print(int x, int y, int highlightOn, std::string_view str) } fontSource.x = chrNum * FONT_WIDTH; if (highlightOn) { - fontSprite = SpriteInfo(highlight->display, &fontSource, &fontDims); + fontSprite = SpriteInfo(highlight->displayTexture, &fontSource, &fontDims); } else { fontSprite = SpriteInfo(fontTexture->raw, &fontSource, &fontDims); @@ -221,7 +221,7 @@ static int Menu_Text_CycleIter(uint8_t r_val, uint8_t g_val, uint8_t b_val) { } // update the palette - highlight->update(highlightPal); + highlight->updateTexture(highlightPal); // SCL_SetColRam(SCL_NBG1, 16, 16, highlightPal); return (r == r_val) && (g == g_val) && (b == b_val); diff --git a/palette.cpp b/palette.cpp index 102e892..6578799 100644 --- a/palette.cpp +++ b/palette.cpp @@ -37,17 +37,34 @@ void Palette::init(SDL_Surface *surface, std::vector &colorPal) { return; } + // surface to write to & display + displaySurface = SDL_CreateRGBSurfaceWithFormat(0, surface->w, surface->h, 32, gPaletteFormat->format); + if (displaySurface == NULL) { + ERR_PRINTF("Error creating surface: %s\n", SDL_GetError()); + return; + } + + if (SDL_SetSurfaceBlendMode(displaySurface, SDL_BLENDMODE_BLEND) < 0) { + ERR_PRINTF("Error setting blend mode: %s\n", SDL_GetError()); + SDL_FreeSurface(displaySurface); + return; + } + // copy the image to the new surface + SDL_BlitSurface(original, NULL, displaySurface, NULL); + // texture to write to & display - display = SDL_CreateTexture(gRenderer, gPaletteFormat->format, + displayTexture = SDL_CreateTexture(gRenderer, gPaletteFormat->format, SDL_TEXTUREACCESS_STREAMING, surface->w, surface->h); - if (display == NULL) { + if (displayTexture == NULL) { ERR_PRINTF("Error creating texture: %s\n", SDL_GetError()); + SDL_FreeSurface(displaySurface); return; } - if (SDL_SetTextureBlendMode(display, SDL_BLENDMODE_BLEND) < 0) { + if (SDL_SetTextureBlendMode(displayTexture, SDL_BLENDMODE_BLEND) < 0) { ERR_PRINTF("Error setting blend mode: %s\n", SDL_GetError()); - SDL_DestroyTexture(display); + SDL_FreeSurface(displaySurface); + SDL_DestroyTexture(displayTexture); return; } @@ -77,14 +94,15 @@ Palette::Palette(const std::string &filename, std::vector &colorPal) { Palette::~Palette() { SDL_FreeSurface(original); - SDL_DestroyTexture(display); + SDL_DestroyTexture(displayTexture); + SDL_FreeSurface(displaySurface); } -void Palette::update(std::vector &newColors) { +void Palette::updateTexture(std::vector &newColors) { uint32_t *pixels; int pitch; - if (SDL_LockTexture(display, NULL, (void **)&pixels, &pitch) < 0) { + if (SDL_LockTexture(displayTexture, NULL, (void **)&pixels, &pitch) < 0) { ERR_PRINTF("Error locking texture: %s\n", SDL_GetError()); } @@ -94,6 +112,15 @@ void Palette::update(std::vector &newColors) { } - SDL_UnlockTexture(display); + SDL_UnlockTexture(displayTexture); +} + +void Palette::updateSurface(std::vector &newColors) { + uint32_t *pixels = (uint32_t *)displaySurface->pixels; + + for (COLOR_VAL &color : colors) { + pixels[color.offset] = newColors[color.colorIndex]; + + } } diff --git a/palette.h b/palette.h index 4c9324c..afb59bd 100644 --- a/palette.h +++ b/palette.h @@ -33,7 +33,8 @@ class Palette { std::vector colors; void init(SDL_Surface *surface, std::vector &colorPal); public: - SDL_Texture *display; + SDL_Texture *displayTexture; + SDL_Surface *displaySurface; // creates from a surface. Handles conversion internally, doesn't free // the surface. // surface: The surface to create the struct from @@ -45,5 +46,7 @@ class Palette { Palette(const std::string &filename, std::vector &colorPal); ~Palette(); // updates the texture with new colors - void update(std::vector &newColors); + void updateTexture(std::vector &newColors); + // updates the surface with new colors + void updateSurface(std::vector &newColors); }; diff --git a/raster.cpp b/raster.cpp index 3c21fb1..007b719 100644 --- a/raster.cpp +++ b/raster.cpp @@ -24,23 +24,35 @@ #include "globals.h" #include "path.h" #include "raster.h" +#include "util.h" -Raster::Raster(const std::string &filename) { - // load image - SDL_Surface *loadSurface = IMG_Load(Path_Graphics("oldlogo.png").c_str()); - if (loadSurface == NULL) { - throw std::runtime_error(SDL_GetError()); - } - // convert to a consistent pixel format - srcSurface = std::make_unique(SDL_ConvertSurfaceFormat(loadSurface, gPaletteFormat->format, 0)); - if (srcSurface->raw == NULL) { - throw std::runtime_error(SDL_GetError()); - } - SDL_FreeSurface(loadSurface); - - // make the output surfaces - dstSurface = std::make_unique(SDL_CreateRGBSurfaceWithFormat(0, - WINDOW_WIDTH, WINDOW_HEIGHT, 32, srcSurface->raw->format->format)); +void Raster::init(const std::string &filename, std::vector *palette) { + // load image + if (palette) { + srcPalette = std::make_unique(filename, *palette); + srcSurface = nullptr; + // make the output surface + dstSurface = std::make_unique(SDL_CreateRGBSurfaceWithFormat(0, + WINDOW_WIDTH, WINDOW_HEIGHT, 32, srcPalette->displaySurface->format->format)); + } + else { + SDL_Surface *loadSurface = IMG_Load(Path_Graphics("oldlogo.png").c_str()); + if (loadSurface == NULL) { + throw std::runtime_error(SDL_GetError()); + } + // convert to a consistent pixel format + srcSurface = std::make_unique(SDL_ConvertSurfaceFormat(loadSurface, gPaletteFormat->format, 0)); + if (srcSurface->raw == NULL) { + throw std::runtime_error(SDL_GetError()); + } + SDL_FreeSurface(loadSurface); + srcPalette = nullptr; + // make the output surface + dstSurface = std::make_unique(SDL_CreateRGBSurfaceWithFormat(0, + WINDOW_WIDTH, WINDOW_HEIGHT, 32, srcSurface->raw->format->format)); + } + + // make the other surfaces if (dstSurface == NULL) { throw std::runtime_error(SDL_GetError()); } @@ -49,7 +61,7 @@ Raster::Raster(const std::string &filename) { } dispSurface = std::make_unique(SDL_CreateRGBSurfaceWithFormat(0, - WINDOW_WIDTH, WINDOW_HEIGHT, 32, srcSurface->raw->format->format)); + WINDOW_WIDTH, WINDOW_HEIGHT, 32, dstSurface->raw->format->format)); if (dispSurface == NULL) { throw std::runtime_error(SDL_GetError()); } @@ -58,8 +70,8 @@ Raster::Raster(const std::string &filename) { } // make the output texture - dispTexture = std::make_unique(SDL_CreateTexture(gRenderer, srcSurface->raw->format->format, SDL_TEXTUREACCESS_STREAMING, - dispSurface->raw->w, dispSurface->raw->h)); + dispTexture = std::make_unique(SDL_CreateTexture(gRenderer, dispSurface->raw->format->format, + SDL_TEXTUREACCESS_STREAMING, dispSurface->raw->w, dispSurface->raw->h)); if (dispTexture == NULL) { throw std::runtime_error(SDL_GetError()); } @@ -68,6 +80,14 @@ Raster::Raster(const std::string &filename) { } } +Raster::Raster(const std::string &filename) { + init(filename, NULL); +} + +Raster::Raster(const std::string &filename, std::vector &palette) { + init(filename, &palette); +} + void Raster::copyToTexture(SDL_Surface *source) { uint32_t *srcPixels = (uint32_t *)source->pixels; uint32_t *dstPixels = nullptr; @@ -85,8 +105,21 @@ void Raster::copyToTexture(SDL_Surface *source) { SDL_UnlockTexture(dispTexture->raw); } +void Raster::updatePalette(std::vector &newColors) { + if (srcPalette) { + srcPalette->updateSurface(newColors); + } +} + void Raster::disp(float x, float y, std::array &lines) { SDL_Rect srcRect, dstRect; + SDL_Surface *from; + if (srcPalette) { + from = srcPalette->displaySurface; + } + else { + from = srcSurface->raw; + } // clear the surfaces SDL_FillRect(dstSurface->raw, NULL, SDL_MapRGBA(dstSurface->raw->format, 0, 0, 0, 0)); @@ -96,8 +129,8 @@ void Raster::disp(float x, float y, std::array &line srcRect = { .x = 0, .y = 0, - .w = srcSurface->raw->w, - .h = srcSurface->raw->h + .w = from->w, + .h = from->h }; dstRect = { .x = (int)x, @@ -105,7 +138,7 @@ void Raster::disp(float x, float y, std::array &line .w = srcRect.w, .h = srcRect.h }; - SDL_BlitSurface(srcSurface->raw, &srcRect, dstSurface->raw, &dstRect); + SDL_BlitSurface(from, &srcRect, dstSurface->raw, &dstRect); // copy each scanline from the destination surface to the display surface srcRect = { diff --git a/raster.h b/raster.h index 20d3371..1b7ec0f 100644 --- a/raster.h +++ b/raster.h @@ -21,7 +21,10 @@ #include #include #include +#include + #include "globals.h" +#include "palette.h" #include "sdl_wrappers.h" typedef struct { @@ -32,12 +35,18 @@ typedef struct { class Raster { private: std::unique_ptr srcSurface; + std::unique_ptr srcPalette; std::unique_ptr dstSurface; std::unique_ptr dispSurface; std::unique_ptr dispTexture; + void init(const std::string &filename, std::vector *palette); void copyToTexture(SDL_Surface *source); public: Raster(const std::string &filename); + Raster(const std::string &filename, std::vector &palette); + // writes the palette to the raster graphic + void updatePalette(std::vector &newColors); + // displays the raster graphic onscreen at the given coordinates void disp(float x, float y, std::array &lines); }; \ No newline at end of file -- 2.45.2