~ndiddy/libremo

dc1f720afafb299b7d43d8f7dfe39b2f3a1e9b79 — Nathan Misner 2 years ago f8a18ed
started work on bonus text
5 files changed, 124 insertions(+), 2 deletions(-)

M Makefile
A bonus.cpp
A bonus.h
M fade.cpp
M game.cpp
M Makefile => Makefile +2 -1
@@ 13,13 13,14 @@ LDFLAGS = $(SDL2_LDFLAGS) -lSDL2_image -lm
OBJECTS =	main.o \
			anim.o \
			ball.o \
			barrier.o \
			block.o \
			block_crack.o \
			block_explode.o \
			block_shine.o \
			block_shrink.o \
			block_stretch.o \
			barrier.o \
			bonus.o \
			capsule.o \
			chip.o \
			cutscene.o \

A bonus.cpp => bonus.cpp +88 -0
@@ 0,0 1,88 @@
/* bonus.cpp: Displays post-level bonus scores
*  Copyright (C) 2022 Nathan Misner
*
*  This file is part of LibreMO.
*
*  LibreMO is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  LibreMO is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with LibreMO.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <memory>

#include "bonus.h"
#include "game.h"
#include "sprite.h"

// each character is 16x16
#define BONUS_TEXT_SIZE (16)
// letters
#define BONUS_S (0)
#define BONUS_T (1)
#define BONUS_A (2)
#define BONUS_G (3)
#define BONUS_E (4)
#define BONUS_C (5)
#define BONUS_L (6)
#define BONUS_R (7)
#define BONUS_B (8)
#define BONUS_O (9)
#define BONUS_N (10)
#define BONUS_U (11) 
// where the 0 digit is on the spritesheet
#define BONUS_NUMBERS_START (12)
static std::shared_ptr<SpriteInfo> textSprite;

void Bonus_Init() {
    textSprite = std::make_unique<SpriteInfo>("bonustext.png");
    textSprite->source.h = BONUS_TEXT_SIZE;
}

static void Bonus_Print(int num, float x, float y) {
    textSprite->x = x;
    textSprite->y = y;
    textSprite->source.y = num * BONUS_TEXT_SIZE;
    textSprite->draw();
}

// the x/y coordinates should be for the rightmost digit
static void Bonus_PrintScore(int score, float x, float y) {
    while (score > 0) {
        int digit = score % 10;
        Bonus_Print(BONUS_NUMBERS_START + digit, x, y);
        score /= 10;
        x -= BONUS_TEXT_SIZE;
    }
}

void Bonus_Normal(int score) {
    float xCursor = LEFT_WALL + BONUS_TEXT_SIZE;
    float yCursor = TOP_WALL + (4 * BONUS_TEXT_SIZE);

    // print the text
    Bonus_Print(BONUS_C, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_L, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_E, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_A, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_R, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    xCursor += BONUS_TEXT_SIZE; // space
    Bonus_Print(BONUS_B, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_O, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_N, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_U, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;
    Bonus_Print(BONUS_S, xCursor, yCursor); xCursor += BONUS_TEXT_SIZE;

    // print the score
    xCursor = LEFT_WALL + (8 * BONUS_TEXT_SIZE);
    yCursor += 2 * BONUS_TEXT_SIZE;
    Bonus_PrintScore(score, xCursor, yCursor);
}
\ No newline at end of file

A bonus.h => bonus.h +25 -0
@@ 0,0 1,25 @@
/* bonus.h: Displays post-level bonus scores
*  Copyright (C) 2022 Nathan Misner
*
*  This file is part of LibreMO.
*
*  LibreMO is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  LibreMO is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with LibreMO.  If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once

// should be run before displaying any bonus
void Bonus_Init();

// shows a normal "CLEAR BONUS" bonus with the given score
void Bonus_Normal(int score);
\ No newline at end of file

M fade.cpp => fade.cpp +1 -1
@@ 1,5 1,5 @@
/* fade.cpp: Fade in/out handler
*  Copyright (C) 2021 Nathan Misner
*  Copyright (C) 2022 Nathan Misner
*
*  This file is part of LibreMO.
*

M game.cpp => game.cpp +8 -0
@@ 23,6 23,7 @@
#include "ball.h"
#include "barrier.h"
#include "block.h"
#include "bonus.h"
#include "capsule.h"
#include "enemy.h"
#include "fade.h"


@@ 229,6 230,8 @@ void Game_Init(int level, int numLevels) {
    Barrier_Init();
    // init enemies
    Enemy_Init();
    // init bonus text
    Bonus_Init();
    //add first ball
    Ball_Add(0, 0, 45);
    gameLevelNum = level;


@@ 585,6 588,7 @@ int Game_Run() {
    // move the ship to the center of the playfield in preparation for the
    // flying out animation
    case STATE_GAME_CENTERSHIP:
        Bonus_Normal(1000);
        if (shipSprite.x < SHIP_STARTX) {
            shipSprite.x += 3;
            if (shipSprite.x > SHIP_STARTX) {


@@ 606,6 610,7 @@ int Game_Run() {

    // door at the top opens to allow the ship to pass through
    case STATE_GAME_SHIPDOOROPEN:
        Bonus_Normal(1000);
        if (Hud_OpenShipDoor()) {
            state = STATE_GAME_FLYUP;
        }


@@ 613,6 618,7 @@ int Game_Run() {

    // ship moves up while transforming from a paddle to a ship
    case STATE_GAME_FLYUP:
        Bonus_Normal(1000);
        if (shipSprite.y > SHIP_FLY1) {
            shipSprite.y--;
            // get the ship ready for animation


@@ 631,6 637,7 @@ int Game_Run() {

    // ship flies down with a small flame coming out the back
    case STATE_GAME_FLYDOWN:
        Bonus_Normal(1000);
        if (shipSprite.y < SHIP_FLY3) {
            shipSprite.y += 0.5;
        }


@@ 648,6 655,7 @@ int Game_Run() {

    // ship flies offscreen with a large flame coming out the back
    case STATE_GAME_FLYOUT:
        Bonus_Normal(1000);
        shipSpeed += 0.2;
        shipSprite.y -= shipSpeed;