git repos / blockattack-game

commit c6bde3e3

sago007 · 2015-12-22 16:56
c6bde3e399508b474064979d4cf5d1c5f255ec0f patch · browse files
parent 82da8a1cd62f07f8c5af3a9e25f13d09912b9150

More changes... still a LONG way to go

Changed files

M source/code/main.cpp before
M source/code/mainVars.hpp before
M source/code/sago/SagoSprite.cpp before
M source/code/sago/SagoSprite.hpp before
diff --git a/source/code/main.cpp b/source/code/main.cpp index 2a1ccb4..6b58f25 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -39,7 +39,7 @@ http://blockattack.sf.net
#define WITH_SDL 1
-
+#include "sago/SagoSpriteHolder.hpp"
#include <iostream>
#include <stdlib.h>
#include <time.h> //Used for srand()
@@ -585,48 +585,21 @@ static string itoa2(int num) {
}
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
-void DrawIMG(SDL_Texture* img, SDL_Renderer* target, int x, int y) {
- SDL_Rect dest;
- dest.x = x;
- dest.y = y;
- dest.w = img->;
- dest.h = SHAPE_SIZE;
-
- SDL_RenderCopy(target, img, nullptr, &dest);
+void DrawIMG(sago::SagoSprite &sprite, SDL_Renderer* target, int x, int y) {
+ sprite.Draw(target,1,x,y);
}
-void DrawIMG_Bounded(SDL_Texture* img, Renderer* target, int x, int y, int minx, int miny, int maxx, int maxy) {
- SDL_Rect dest;
- dest.x = x;
- dest.y = y;
- if (dest.x >= minx && dest.y >= miny && dest.x + img->w <= maxx && dest.y + img->h <= maxy) {
- SDL_BlitSurface(img, nullptr, target, &dest);
- return;
- }
- SDL_Rect dest2;
- dest2.x = 0;
- dest2.y = 0;
- dest2.w = img->w;
- dest2.h = img->h;
- if (dest.x < minx) {
- dest2.x += minx-dest.x;
- dest.x = minx;
- }
- if (dest.y < miny) {
- dest2.y += miny-dest.y;
- dest.y = miny;
- }
- if (dest.y+dest2.h > maxy) {
- dest2.h -= (dest.y+dest2.h - maxy);
- }
- if (dest.x+dest2.w > maxx) {
- dest2.w -= (dest.x+dest2.w - maxx);
- }
- SDL_BlitSurface(img, &dest2, target, &dest);
+void DrawIMG_Bounded(sago::SagoSprite &sprite, SDL_Renderer* target, int x, int y, int minx, int miny, int maxx, int maxy) {
+ SDL_Rect bounds;
+ bounds.x = minx;
+ bounds.y = miny;
+ bounds.w = maxx-minx;
+ bounds.h = maxy-miny;
+ sprite.DrawBounded(target,1,x,y,bounds);
}
/*Draws a part of an image on a surface of choice*/
-void DrawIMG(SDL_Texture* img, Renderer* target, int x, int y, int w, int h, int x2, int y2) {
+/*void DrawIMG(sago::SagoSprite &sprite, Renderer* target, int x, int y, int w, int h, int x2, int y2) {
SDL_Rect dest;
dest.x = x;
dest.y = y;
@@ -635,8 +608,9 @@ void DrawIMG(SDL_Texture* img, Renderer* target, int x, int y, int w, int h, int
dest2.y = y2;
dest2.w = w;
dest2.h = h;
- SDL_BlitSurface(img, &dest2, target, &dest);
-}
+ sprite.Draw()
+
+}*/
void NFont_Write(SDL_Renderer* target, int x, int y, const string& text) {
@@ -944,16 +918,16 @@ public:
topy = ty;
}
- void DrawImgBoard(SDL_Surface* img, int x, int y) const {
+ void DrawImgBoard(sago::SagoSprite& img, int x, int y) const {
DrawIMG(img, screen, x+topx, y+topy);
}
- void DrawImgBoardBounded(SDL_Surface* img, int x, int y) const {
+ void DrawImgBoardBounded(sago::SagoSprite& img, int x, int y) const {
DrawIMG_Bounded(img, screen, x+topx, y+topy, topx, topy, topx + backBoard->w, topy + backBoard->h);
}
void PrintTextCenteredBoard(int x, int y, const char* text) {
- nf_button_font.drawCenter(x+topx+60, y+topy+10, "%s", text);
+ nf_button_font.draw(screen, x+topx+60, y+topy+10, NFont::CENTER, "%s", text);
}
int GetTopX() const {
diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp index 578328e..de70e7e 100644 --- a/source/code/mainVars.hpp +++ b/source/code/mainVars.hpp
@@ -25,6 +25,9 @@ http://blockattack.sf.net
#ifndef _MAINVARS_HPP
#define _MAINVARS_HPP
+#include "sago/SagoSprite.hpp"
+
+
//main variables and constants
@@ -65,7 +68,7 @@ static SDL_Surface *ready[2]; //Before the blocks fall
static SDL_Surface *explosion[4]; //Then a block explodes
//Animations end
static SDL_Surface *counter[3]; //Counts down from 3
-static SDL_Surface *bricks[7]; //The bricks, saved in an array of pointers
+static sago::SagoSprite bricks[7]; //The bricks, saved in an array of pointers
static SDL_Surface *crossover; //Cross the bricks that will be cleared soon
static SDL_Surface *balls[7]; //The balls (the small ones that jump around)
static SDL_Surface *changeButtonsBack;
diff --git a/source/code/sago/SagoSprite.cpp b/source/code/sago/SagoSprite.cpp index 9b7186f..b269dcd 100644 --- a/source/code/sago/SagoSprite.cpp +++ b/source/code/sago/SagoSprite.cpp
@@ -27,13 +27,17 @@
namespace sago {
struct SagoSprite::SagoSpriteData {
- SDL_Texture* tex;
+ SDL_Texture* tex = nullptr;
SDL_Rect imgCord;
SDL_Rect origin = {};
int aniFrames = 0;
int aniFrameTime = 0;
};
+SagoSprite::SagoSprite() {
+ data = new SagoSpriteData();
+}
+
SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) {
data = new SagoSpriteData();
data->tex = texHolder.getTexturePtr(texture);
@@ -42,13 +46,20 @@ SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& textu
data->aniFrameTime = animationFrameLength;
}
+SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*other.data)) {
+
+}
+SagoSprite& operator=(const SagoSprite& base) {
+ *data = *other.data;
+ return *this;
+}
SagoSprite::~SagoSprite() {
delete data;
}
-void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y) const {
+void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const {
SDL_Rect rect = data->imgCord;
rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
SDL_Rect pos = rect;
@@ -57,7 +68,7 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y)
SDL_RenderCopy(target, data->tex, &rect, &pos);
}
-void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y, const SDL_Rect& part) const {
+void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const {
SDL_Rect rect = data->imgCord;
rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
rect.x += part.x;
@@ -70,6 +81,52 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y,
SDL_RenderCopy(target, data->tex, &rect, &pos);
}
+void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
+ SDL_Rect rect = data->imgCord;
+ rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
+ SDL_Rect pos = rect;
+ pos.x = x;
+ pos.y = y;
+ if (pos.x > bounds.x+bounds.w) {
+ return;
+ }
+ if (pos.y > bounds.y+bounds.h) {
+ return;
+ }
+ if (pos.x+pos.w < bounds.x) {
+ return;
+ }
+ if (pos.y+pos.h < bounds.y) {
+ return;
+ }
+ if (pos.x < bounds.x) {
+ Sint16 absDiff = bounds.x-pos.x;
+ pos.x+=absDiff;
+ rect.x+=absDiff;
+ pos.w-=absDiff;
+ rect.w-=absDiff;
+ }
+ if (pos.y < bounds.y) {
+ Sint16 absDiff = bounds.y-pos.y;
+ pos.y+=absDiff;
+ rect.y+=absDiff;
+ pos.h-=absDiff;
+ rect.h-=absDiff;
+ }
+ if (pos.x+pos.w > bounds.x+bounds.w) {
+ Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
+ pos.w -= absDiff;
+ rect.w -= absDiff;
+ }
+ if (pos.y+pos.h > bounds.y+bounds.h) {
+ Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
+ pos.h -= absDiff;
+ rect.h -= absDiff;
+ }
+
+ SDL_RenderCopy(target, data->tex, &rect, &pos);
+}
+
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
data->origin = newOrigin;
}
diff --git a/source/code/sago/SagoSprite.hpp b/source/code/sago/SagoSprite.hpp index b6f31d5..8443192 100644 --- a/source/code/sago/SagoSprite.hpp +++ b/source/code/sago/SagoSprite.hpp
@@ -31,6 +31,7 @@ namespace sago {
class SagoSprite {
public:
+ SagoSprite();
SagoSprite(const SagoDataHolder &texHolder, const std::string &texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength);
/**
* Draws the sprite to a given render window
@@ -39,7 +40,7 @@ public:
* @param x Place to draw the sprite
* @param y Place to draw the sprite
*/
- void Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y) const;
+ void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const;
/**
* Draws part of the sprite to a given render window
* @param target The render window to draw on
@@ -48,16 +49,25 @@ public:
* @param y Place to draw the sprite
* @param part the part of the sprite that should be drawn.
*/
- void Draw(SDL_Renderer* target, Sint32 frameTime, float x, float y, const SDL_Rect& part) const;
+ void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const;
+ /**
+ * Draws the wprite to the given renderer but makes sure to not draw outside th bounds given
+ * @param target The render window to draw on
+ * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation
+ * @param x Place to draw the sprite
+ * @param y Place to draw the sprite
+ * @param bounds A recagular area that we must not draw outside.
+ */
+ void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const;
/**
* Set a different origin. Normally it is the top left cornor. But in some cases you might want to center the origin or tranform it for other reasons
* @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default
*/
void SetOrigin(const SDL_Rect& newOrigin);
+ SagoSprite(const SagoSprite& base);
+ SagoSprite& operator=(const SagoSprite& base);
virtual ~SagoSprite();
private:
- SagoSprite(const SagoSprite& base) = delete;
- SagoSprite& operator=(const SagoSprite& base) = delete;
struct SagoSpriteData;
SagoSpriteData *data;
};