Migrated to C++ (phew)

Made sprite class
This commit is contained in:
breadone 2023-03-11 16:33:16 +13:00
parent c9056466a8
commit d9628233ce
4 changed files with 124 additions and 42 deletions

56
source/Sprite.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "Sprite.h"
Sprite::Sprite() {
// C2D_SpriteSetCenter(&this->spr, 0.5f, 0.5f);
// C2D_SpriteSetPos(&this->spr, 400/2, 240/2);
this->x = 200; this->y = 120; this->spr = new C2D_Sprite;
}
Sprite::Sprite(C2D_Sprite* spr, float x, float y) {
this->x = x; this->y = y; this->spr = spr;
C2D_SpriteSetCenter(this->spr, 0.5f, 0.5f);
C2D_SpriteSetPos(this->spr, x, y);
}
void Sprite::setPosition(float x, float y) {
this->x = x;
this->y = y;
C2D_SpriteSetPos(this->spr, x, y);
}
void Sprite::move(float dx, float dy) {
if (this->x >= 400 || this->x < 1 || this->y >= 240 || this->x < 1) {
return;
}
this->x += dx;
this->y += dy;
C2D_SpriteSetPos(this->spr, this->x, this->y);
}
void Sprite::setCenter(float x, float y) {
C2D_SpriteSetCenter(this->spr, x, y);
}
void Sprite::draw() {
C2D_DrawSprite(this->spr);
}
float Sprite::getPosX() {
return this->x;
}
float Sprite::getPosY() {
return this->y;
}
C2D_Sprite* Sprite::getSpr() {
return this->spr;
}
void Sprite::setSpr(C2D_Sprite* newSpr) {
this->spr = newSpr;
}

36
source/Sprite.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#ifndef SPRITE_H
#define SPRITE_H
#include <citro2d.h>
class Sprite {
public:
Sprite();
Sprite(C2D_Sprite* spr, float x, float y);
void setPosition(float x, float y);
void move(float dx, float dy);
void setCenter(float x, float y);
void draw();
float getPosX();
float getPosY();
C2D_Sprite* getSpr();
void setSpr(C2D_Sprite* newSpr);
C2D_Sprite* spr;
private:
float x, y;
};
#endif

View File

@ -6,28 +6,20 @@
*/
#include <citro2d.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include "Sprite.h"
#include "sprites.h"
#include "SpriteList.h"
#define MAX_SPRITES 768
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 240
typedef struct {
C2D_Sprite spr;
float x, y; // pos
} Sprite;
// init spritesheet
static C2D_SpriteSheet spriteSheet;
static Sprite sprites[MAX_SPRITES];
Sprite sprites[MAX_SPRITES];
static void initSprites() {
size_t numImages = C2D_SpriteSheetCount(spriteSheet);
@ -37,36 +29,24 @@ static void initSprites() {
float x = SCREEN_WIDTH / 2;
float y = SCREEN_HEIGHT / 2;
C2D_SpriteFromSheet(&thisSprite->spr, spriteSheet, i);
C2D_SpriteSetCenter(&thisSprite->spr, 0.5f, 0.5f);
C2D_SpriteSetPos(&thisSprite->spr, x, y);
thisSprite->x = x;
thisSprite->y = y;
C2D_SpriteFromSheet(thisSprite->spr, spriteSheet, i);
C2D_SpriteSetCenter(thisSprite->spr, 0.5f, 0.5f);
thisSprite->setPosition(x, y);
}
// set bird consts
C2D_SpriteSetPos(&sprites[SPR_BIRD].spr, SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3);
sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3);
// set bottom screen scorecard
C2D_SpriteSetCenter(&sprites[SPR_SCORECARD].spr, 0.0, 0.0);
C2D_SpriteSetPos(&sprites[SPR_SCORECARD].spr, 2, 40);
sprites[SPR_SCORECARD].setCenter(0.0, 0.0);
sprites[SPR_SCORECARD].setPosition(2, 40);
sprites[SPR_PIPETOP].setPosition(280, 14);
sprites[SPR_PIPEBOTTOM].setPosition(280, 200);
}
void moveSprite(Sprite *spr, s16 dx, s16 dy) {
// stop sprite going OOB
if (spr->x >= SCREEN_WIDTH || spr->x < 1 || spr->y >= SCREEN_HEIGHT || spr->x < 1) {
return;
}
spr->x += dx;
spr->y += dy;
C2D_SpriteSetPos(&spr->spr, spr->x, spr->y);
}
void drawSprite(int id) {
C2D_DrawSprite(&sprites[id].spr);
}
int main(int argc, char* argv[]) {
@ -106,8 +86,8 @@ int main(int argc, char* argv[]) {
float v = 0; // velocity
// set bg properties
C2D_SpriteSetPos(&sprites[SPR_BG].spr, 200, 120); // center bg
C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res)
sprites[SPR_BG].setPosition(200, 120);
C2D_SpriteSetScale(sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res)
size_t score = 0;
@ -123,12 +103,16 @@ int main(int argc, char* argv[]) {
// gravity calcs
v += a;
moveSprite(&sprites[SPR_BIRD], 0, v);
C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8);
sprites[SPR_BIRD].move(0, v);
C2D_SpriteSetRotationDegrees(sprites[SPR_BIRD].spr, v*9.8);
sprites[SPR_PIPETOP].move(-0.2, 0);
sprites[SPR_PIPEBOTTOM].move(-0.2, 0);
if (kDown & KEY_A) {
v = -5.5;
moveSprite(&sprites[SPR_BIRD], 0, v);
// moveSprite(&sprites[SPR_BIRD], 0, v);
sprites[SPR_BIRD].move(0, v);
// temp score = num of flaps
score++;
}
@ -144,12 +128,18 @@ int main(int argc, char* argv[]) {
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(top, C3D_CLEAR_COLOR);
C2D_SceneBegin(top);
drawSprite(SPR_BG);
drawSprite(SPR_BIRD);
// drawSprite(SPR_BG);
// drawSprite(SPR_BIRD);
sprites[SPR_BG].draw();
sprites[SPR_BIRD].draw();
// drawSprite(SPR_PIPEBOTTOM);
// drawSprite(SPR_PIPETOP);
C2D_TargetClear(bottom, C2D_Color32f(0.3294f, 0.7529f, 0.7882f, 1.0f));
C2D_SceneBegin(bottom);
drawSprite(SPR_SCORECARD);
// drawSprite(SPR_SCORECARD);
sprites[SPR_SCORECARD].draw();
// C2D_DrawSprite(&scoreSprite->spr);