starting doing this :3

This commit is contained in:
pradyun
2023-09-15 01:04:00 +12:00
parent e5e5342a6c
commit 6fc3def7a0
6 changed files with 414 additions and 159 deletions

View File

@@ -0,0 +1,39 @@
#include "Level.hpp"
template <typename T>
Level::Level(T* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom) : level(levelName), top(top), bottom(bottom) {
this->spritesheet = C2D_SpriteSheetLoad(spritesheetPath);
size_t numImages = C2D_SpriteSheetCount(spritesheet);
for (size_t i = 0; i < numImages; i++) {
Sprite* thisSprite = &sprites[i];
float x = SCREEN_WIDTH / 2;
float y = SCREEN_HEIGHT / 2;
C2D_SpriteFromSheet(&thisSprite->spr, spritesheet, i);
C2D_SpriteSetCenter(&thisSprite->spr, 0.5f, 0.5f);
thisSprite->setPosition(x, y);
}
}
void Level::run() {
if (!hasBeenSetup) {
level->setup();
this->hasBeenSetup = true;
}
level->update();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C2D_TargetClear(top, C3D_CLEAR_COLOR);
C2D_SceneBegin(top);
level->drawTop();
C2D_TargetClear(bottom, C3D_CLEAR_COLOR);
C2D_SceneBegin(bottom);
level->drawBottom();
C3D_FrameEnd(0);
}
void Level::cleanup() {
C2D_SpriteSheetFree(spriteSheet);
}

View File

@@ -0,0 +1,75 @@
/*
Level.hpp
Bakery2D
Created by breadone
*/
#pragma once
#ifndef LEVEL_HPP
#define LEVEL_HPP
#include <citro2d.h>
#include "../Sprite/Sprite.hpp"
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 240
#define MAX_SPRITES 768
/**
The Level class, the backdrops for your game, and the gateway to how they are controlled.
Example:
```cpp
struct mainLevel: Level {
mainLevel(): Level(this, "romfs:/gfx/mainlevel/sprites.t3x");
int score = 0;
bool gameOver = false;
void setup() {
// set background sprite to correct location and scale
sprites[0].setPosition(200, 120);
C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res)
}
void update() {
sprites[1].move(0, -0.4); // move bird down
sprites[2].move(-1, 0); // move pipe left
// other game logic goes here...
}
void drawTop() {
sprites[0].draw(); // draw background
sprites[1].draw(); // draw bird
sprites[2].draw(); // draw pipe
}
void drawBottom() {
sprites[3].draw(); // draw scorecard
}
};
```
*/
class Level {
public:
Sprite sprites[MAX_SPRITES];
C2D_SpriteSheet spritesheet;
C3D_RenderTarget* top;
C3D_RenderTarget* bottom;
Level* level;
bool hasBeenSetup = false;
virtual void setup() = 0;
virtual void update() = 0;
virtual void drawTop() = 0;
virtual void drawBottom() = 0;
template <typename T>
Level(T* levelName, char* spritesheetPath, C3D_RenderTarget* top, C3D_RenderTarget* bottom);
// sets up the level if it hasnt been, then updates the logic, and draws the frame
void run();
virtual void cleanup();
};
#endif

View File

@@ -0,0 +1,106 @@
#include "Sprite.hpp"
Sprite::Sprite() {
// C2D_SpriteSetCenter(&this->spr, 0.5f, 0.5f);
// C2D_SpriteSetPos(&this->spr, 400/2, 240/2);
this->x = 200; this->y = 120;
}
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, bool overrideOOB) {
if (!overrideOOB) {
// make sure translate doesnt move to OOB
const float newX = this->x + dx;
const float newY = this->y + dy;
if (!((unsigned)(newX-1) <= 398 && (unsigned)(newY-1) <= 238)) {
return;
}
}
this->x += dx;
this->y += dy;
C2D_SpriteSetPos(&this->spr, this->x, this->y);
}
void Sprite::setCenter(float x, float y) {
this->center[0] = x; this->center[1] = y;
C2D_SpriteSetCenter(&this->spr, x, y);
}
void Sprite::setHitbox(float w, float h) {
this->width = w; this->height = h;
}
bool Sprite::isCollidingWith(Sprite &other) {
// hitbox not set
if (this->width == 0 && this->height == 0) return false;
const float thisHalfWidth = this->width/2;
const float otherHalfWidth = other.width/2;
const float thisHalfHeight = this->height/2;
const float otherHalfHeight = other.height/2;
// check x collision
if (this->x + thisHalfWidth >= other.x - otherHalfWidth ||
this->x - thisHalfWidth <= other.x + otherHalfWidth
) {
return true;
}
// check y collision
if (this->y + thisHalfHeight >= other.y - otherHalfHeight ||
this->y - thisHalfHeight <= other.y + otherHalfHeight
) {
return true;
}
return false;
}
float* Sprite::getCenter() {
return this->center;
}
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;
}
void Sprite::copy(Sprite* other) {
memcpy(&other->x, &this->x, sizeof(this->x));
memcpy(&other->y, &this->y, sizeof(this->y));
memcpy(&other->width, &this->width, sizeof(this->width));
memcpy(&other->height, &this->height, sizeof(this->height));
memcpy(other->center, this->center, sizeof(this->center));
memcpy(&other->spr, &this->spr, sizeof(this->spr));
}

View File

@@ -0,0 +1,46 @@
#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, bool overrideOOB=false);
void setCenter(float x, float y);
void setHitbox(float width, float height);
bool isCollidingWith(Sprite &other);
void draw();
float getPosX();
float getPosY();
float* getCenter();
C2D_Sprite* getSpr();
void setSpr(C2D_Sprite* newSpr);
void copy(Sprite* other);
C2D_Sprite spr;
private:
float x, y;
float width, height; // for hitbox
float center[2];
};
#endif