From 5421c812b9fc64371c7f8ce3886b0b091eef458f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 5 Aug 2012 17:09:26 +0200 Subject: [PATCH] added simple sprite class --- Debug/makefile | 1 + Debug/sources.mk | 1 + Debug/src/graphics/subdir.mk | 24 +++++++++++++++++++++++ Release/makefile | 1 + Release/sources.mk | 1 + Release/src/graphics/subdir.mk | 24 +++++++++++++++++++++++ src/battle/Monster.h | 6 ++++-- src/graphics/Sprite.cpp | 25 ++++++++++++++++++++++++ src/graphics/Sprite.h | 35 ++++++++++++++++++++++++++++++++++ 9 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 Debug/src/graphics/subdir.mk create mode 100644 Release/src/graphics/subdir.mk create mode 100644 src/graphics/Sprite.cpp create mode 100644 src/graphics/Sprite.h diff --git a/Debug/makefile b/Debug/makefile index 3f18039..507437a 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -9,6 +9,7 @@ RM := rm -rf # All of the sources participating in the build are defined here -include sources.mk -include src/sdl/subdir.mk +-include src/graphics/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index 92d6efe..0fdadd3 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -25,6 +25,7 @@ C_UPPER_DEPS := SUBDIRS := \ src/sdl \ src \ +src/graphics \ src/battle \ src/app \ diff --git a/Debug/src/graphics/subdir.mk b/Debug/src/graphics/subdir.mk new file mode 100644 index 0000000..63083ec --- /dev/null +++ b/Debug/src/graphics/subdir.mk @@ -0,0 +1,24 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/graphics/Sprite.cpp + +OBJS += \ +./src/graphics/Sprite.o + +CPP_DEPS += \ +./src/graphics/Sprite.d + + +# Each subdirectory must supply rules for building sources it contributes +src/graphics/%.o: ../src/graphics/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/usr/include/SDL -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Release/makefile b/Release/makefile index 3f18039..507437a 100644 --- a/Release/makefile +++ b/Release/makefile @@ -9,6 +9,7 @@ RM := rm -rf # All of the sources participating in the build are defined here -include sources.mk -include src/sdl/subdir.mk +-include src/graphics/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Release/sources.mk b/Release/sources.mk index 92d6efe..0fdadd3 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -25,6 +25,7 @@ C_UPPER_DEPS := SUBDIRS := \ src/sdl \ src \ +src/graphics \ src/battle \ src/app \ diff --git a/Release/src/graphics/subdir.mk b/Release/src/graphics/subdir.mk new file mode 100644 index 0000000..6325d12 --- /dev/null +++ b/Release/src/graphics/subdir.mk @@ -0,0 +1,24 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/graphics/Sprite.cpp + +OBJS += \ +./src/graphics/Sprite.o + +CPP_DEPS += \ +./src/graphics/Sprite.d + + +# Each subdirectory must supply rules for building sources it contributes +src/graphics/%.o: ../src/graphics/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/usr/include/SDL -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/src/battle/Monster.h b/src/battle/Monster.h index adebbb2..0b35d31 100644 --- a/src/battle/Monster.h +++ b/src/battle/Monster.h @@ -10,6 +10,8 @@ #include +namespace graphics { class Sprite; } + namespace battle { class Monster { @@ -21,7 +23,7 @@ public: public: const char *Name() const { return name; } Uint8 Level() const { return level; } - const /* Sprite */ void *Sprite() const { return sprite; } + const graphics::Sprite *Sprite() const { return sprite; } Uint16 MaxHealth() const { return maxHealth; } Uint16 Health() const { return health; } @@ -49,7 +51,7 @@ public: private: const char *name; - /* Sprite */ void *sprite; + graphics::Sprite *sprite; /* Item */ void *dropItem; /* Script */ void *attackScript; /* Script */ void *defenseScript; diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp new file mode 100644 index 0000000..67deeef --- /dev/null +++ b/src/graphics/Sprite.cpp @@ -0,0 +1,25 @@ +/* + * Sprite.cpp + * + * Created on: Aug 5, 2012 + * Author: holy + */ + +#include "Sprite.h" + +namespace graphics { + +void Sprite::Draw(SDL_Surface *dest, int x, int y, int col, int row) const { + SDL_Rect srcRect, destRect; + srcRect.x = col * Width(); + srcRect.y = row * Height(); + srcRect.w = Width(); + srcRect.h = Height(); + destRect.x = x; + destRect.y = y; + destRect.w = Width(); + destRect.h = Height(); + SDL_BlitSurface(surface, &srcRect, dest, &destRect); +} + +} diff --git a/src/graphics/Sprite.h b/src/graphics/Sprite.h new file mode 100644 index 0000000..5710ee5 --- /dev/null +++ b/src/graphics/Sprite.h @@ -0,0 +1,35 @@ +/* + * Sprite.h + * + * Created on: Aug 5, 2012 + * Author: holy + */ + +#ifndef GRAPHICS_SPRITE_H_ +#define GRAPHICS_SPRITE_H_ + +#include + +namespace graphics { + +class Sprite { + +public: + Sprite(SDL_Surface *s, int width, int height) + : surface(s), width(width), height(height) { } + +public: + int Width() const { return width; } + int Height() const { return height; } + void Draw(SDL_Surface *dest, int x, int y, int col = 0, int row = 0) const; + +private: + SDL_Surface *surface; + int width; + int height; + +}; + +} + +#endif /* GRAPHICS_SPRITE_H_ */ -- 2.39.2