All pastes #360722 Raw Edit

Stle

public cpp v1 · immutable
·published 2007-02-17 18:30 UTC
rendered paste body
#ifndef _zcolor_hpp_#define _zcolor_hpp_namespace Zen {        /**         * The Color class stores colors as RGB-values.         */        struct Color {                /**                 * Construct a Color instance based on RGB-values.                 * @param rv R-value                 * @param gv G-value                 * @param bv B-value                 */                Color(int rv = 0, int gv = 0, int bv = 0);                /**                 * R-value                 */                int r;                /**                 * G-value                 */                int g;                /**                 * B-value                 */                int b;        };}#endif#ifndef _zcoord_hpp_#define _zcoord_hpp_namespace Zen {        /**         * Coord class. Store x and y coordinates.         */        class Coord {                public:                        Coord(int xx, int yy) : xpos(xx), ypos(yy) { }                        Coord() { }                        void x(int xx) { xpos = xx; }                        void y(int yy) { ypos = yy; }                        int x() { return xpos; }                        int y() { return ypos; }                private:                        int xpos, ypos;        };}#endif#ifndef _zdisplay_hpp_#define _zdisplay_hpp_#include <vector>#include "screen.hpp"#include "object.hpp"#include "color.hpp"namespace Zen {        class Object;         class Screen;        class Color;        /**         * Display class. This is the display-subsystem which is responsible for         * loading and displaying all the graphics. It is also responsible for         * creating and managing the actual screen.         */        class Display {                public:                        /**                         * Class constructor. Default constructor for the Display class.                         * @param w the width of the screen                         * @param h the height of the screen                         * @param res the screen resolution                          */                        Display(int, int, int = 0);                        /**                         * Clear function. Clear the screen to a given color.                         * @param col is color which the screen will be cleared to.                         */                        void clear(Color);                        /**                         * Register object function. Register an object to the display                         * subsystem, which is later going to be blitted to the screen.                          * @param object is object to be registered. object could be an                         * instance of a subclass derived from Object.                         * @param tile defines in which order the object will be blitted.                         */                         void register_object(Object*);                        /**                         * Show the registered objects by blitting them to the screen.                         */                        void show();                        /**                         * @return The width of the screen                         */                        int width();                        /**                         * @return The height of the screen                         */                        int height();                private:                        Screen screen;                        std::vector<Object*> objects;                        Display();                        Display& operator=(const Display&);        };}#endif#ifndef _zevent_hpp_#define _zevent_hpp_#include "SDL.h"namespace Zen {        /**         * Event class. Provide functions for exctracting information from the         * underlying SDL_Event structure.         */         class Event {                public:                        /**                          * Class constructor. Create an event from a SDL_Event object.                         * This is rarely done by the user of the API, and is most often                         * returned as a result when reading input using the                         * input-subsystem.                         */                        Event(SDL_Event&);                        /**                         * @return It returns an integer describing the type of the event                          * that is being read.                          */                        int type();                        /**                         * @return Returns a char describing what key is being pressed.                          */                        char keydown();                        /**                         * @return Returns an unsigned char* describing the current                         * keystate.                         */                        unsigned char* keystate();                        /**                         * @return Returns true if there has been a mouse motion, and                          * false if it has not changed.                         * motion                         */                        bool mouse_motion();                        /**                         * @return Returns true if a mouse button has been clicked, and                         * false if not.                          */                        bool mouse_clicked();                        /**                         * @return Returns an int holding the x position of the mouse                         * cursor                         */                        int mouse_pos_x();                        /**                         * @return Returns an int holding the y position of the mouse                         * cursor                         */                        int mouse_pos_y();                private:                        SDL_Event event;        };}#endif#ifndef _zexception_hpp_#define _zexception_hpp_#include <string>namespace Zen {        /**         * Exception class. General exception class that will be thrown by zengine         * if an unexcpected error is detected.         */        class Exception {                public:                        /**                         * Class constructor. Constructs an instance of class Exception.                          * @param description describes what kind of problem that has                         * occured                         * @param error describes the actual error message                         */                        Exception(std::string, std::string = "");                        /**                         * @return It returns a const std::string& describng what went                         * wrong during program execution.                         */                        const std::string& what() const;                private:                        std::string message;        };}#endif#ifndef _zinput_hpp_#define _zinput_hpp_#include <sigc++/sigc++.h>#include "SDL.h"#include "event.hpp"namespace Zen {        /**         * Input class. The main component of the input-subsystem.         */        class Input {                public:                        /**                         * Class constructor. Create an instance of the Input-class, which                         * enables you to read input from the user.                         */                        Input();                        enum {POLL, WAIT};                        /**                         * @param type specifies how the reading is done. Currently, you                         * can choose between polling for events, and waiting.                          */                        void read(int type = POLL);                        sigc::signal<void, Event> signal_event;                private:                        SDL_Event event;        };}#endif#ifndef _object_container_hpp_#define _object_container_hpp_#include <vector>#include <sigc++/sigc++.h>#include "object.hpp"namespace Zen {        template<template<class> class T = std::vector>        class ObjectContainer {                public:                        /**                         * @return It returns the underlying array                         */                        T<Object>& array();                        /**                         * Iterate throught the container array, and emits signal_collision                         * if object has collided with any of the objects in the array.                          */                        void collision(Zen::Object& object);                        /**                         * Connect a function to this object to take action whenever an object                         * collides with another object.                         */                        sigc::signal<void, Object&, Object&> signal_collision;                protected:                        T<Object> container;        };}#endif#ifndef _zobject_hpp_#define _zobject_hpp_#include <string>#include "SDL.h"#include "coord.hpp"#include "rect.hpp"namespace Zen {        class Text;        class Color;        /**         * Base class for all objects displayed on the screen. This is         * an interface class, so pure virtual functions must be overloaded         * in derived classes.         */        class Object {                public:                        /**                         * Default constructor. Construct an empty Object.                         */                        Object();                        /**                         * Construct an Object from an image path by loading the                         * image file supplied.                         * @param image_path A std::string containing the path                         * to the image that should be loaded.                         */                        Object(std::string);                        /**                         * Virtual descructorn.                          */                        virtual ~Object();                        /**                         * Load an image from file.                          * @param file path to image ex. /home/user/images/img.jpg                          */                        void load_image(std::string file);                        /**                         * Load text using SDL_ttf.                         * @param text a Text object that determines the font, color, size                         * and the actual text that will be loaded.                         */                        void load_text(Text txt);                        /**                         * Crop the image by setting x, y, width and height values for                         * the object, deciding what part of the image that will be shown.                          * @param rect what part of the image that is going to be visible.                          */                        void set_rect(Rect rect);                        /**                         * Get the rect of the object.                         * @return Returns the rect corresponding to the object, describing                         * what part of the image that is going to be visible.                         */                        Rect get_rect();            /**             * Set the coordinates of the object             * @param coord the Coord object             **/            void set_coordinate(Coord coord);            /**             * Get the coordinates of the object             * @return Returns a Coord object holding the x and y             * positions of the object.              **/            Coord get_coordinate();                        /**                         * Set a colorkey/chromakey based on a Color object, that will                         * prevent pixels with this color from being displayed.                         * @param color the Color object                         */                        void set_colorkey(Color color);                        /**                         * Virtual function for blitting the object to target.                         * @param target The target surface which the object will be                         * blitted to.                         */                        virtual void blit_to(Object*);                        /**                         * @return Returns the current width of the object.                         */                        int width();                        /**                         * @return Returns the current height of the object.                         */                        int height();                protected:                        /**                         * When deriving from Object, this function should be used                         * when blitting member Objects to the object's surface.                         */                        void blit_to_self(Object*);                        SDL_Surface* surface;                        Rect source;                        Coord coord;        };};#endif#ifndef _scrolling_object_hpp_#define _scrolling_object_hpp_#include <string>#include "object.hpp"#include "sprite.hpp"namespace Zen {        namespace View {                /**                 * Class for handeling the scrolling in a typical platform game.                 */                class Platform : public Object {                        public:                                ~Platform() { };                                /**                                 * Direction is an enumerator that will either be LEFT or RIGHT, defining                                 * what direction we wish to scroll.                                 */                                enum Direction {LEFT, RIGHT};                                /**                                 * Set the background layer. This layer will be static, and will not move.                                  * @param bl Path to the background layer.                                 */                                void set_background_layer(std::string bl);                                /**                                 * Set layer 1. This layer will move base_speed pixels when the scroll                                 * function is called.                                 * @param l1 Path to image corresponding to layer 1                                 */                                void set_layer1(std::string l1);                                /**                                 * Set layer 2. This layer will move twice as fast as layer 1.                                 * @param l1 Path to image corresponding to layer 2                                  */                                void set_layer2(std::string l2);                                /**                                 * Set layer 3. This layer will move twice as fast as layer 1.                                 * @param l3 Path to image corresponding to layer 3.                                 */                                void set_layer3(std::string l3);                                /**                                 * Set the base speed. This speed will decide how fast layer 1 will move,                                 * and thereby decide how fast the other layers will move too.                                  */                                void set_base_speed(int bs);                                /**                                 * This function will do the actual scrolling.                                 * @param dir Defines in what direction we wish to scroll.                                 */                                void scroll(Direction);                                /**                                 * Ovverride Object::blit_to, so that all the objects are going to be                                 * blitted..                                 */                                void blit_to(Object*);                        private:                                int base_speed;                                Sprite background_layer;                                Sprite layer1;                                Sprite layer2;                                Sprite layer3;                };        }}#endif#ifndef _zrect_hpp_#define _zrect_hpp_namespace Zen {        class Rect {                public:                        Rect(int xx, int yy, int ww, int hh)                                 : xpos(xx), ypos(yy), wlen(ww), hlen(hh)                         { v_set =  true; }                        Rect() { v_set = false; }                        void x(int xx) { xpos = xx; v_set = true; }                        void y(int yy) { ypos = yy; v_set = true; }                        void w(int ww) { wlen = ww; v_set = true; }                        void h(int hh) { hlen = hh; v_set = true; }                        int x() { return xpos; }                        int y() { return ypos; }                        int w() { return wlen; }                        int h() { return hlen; }                        bool value_set() { return v_set; }                private:                        int xpos, ypos, wlen, hlen;                        bool v_set;        };}#endif#ifndef _zscreen_hpp_#define _zscreen_hpp_#include "SDL.h"#include "object.hpp"namespace Zen {        class Color;        class Screen : public Object {                public:                        void init(int, int, int = SDL_HWSURFACE);                        void fill_rect(Color);                        void flip();        };        enum {                HARDWARE = SDL_HWSURFACE,                 SOFTWARE = SDL_SWSURFACE,                DOUBLEBUF = SDL_DOUBLEBUF,                FULLSCREEN = SDL_FULLSCREEN        };}#endif#ifndef _sprite_hpp_#define _sprite_hpp_#include <vector>#include "object.hpp"#include "coord.hpp"namespace Zen {        class Sprite : public Object {                public:                        ~Sprite() { };                        /**                         * Function for adding a frame to the sprite, making                         * it possible to animate sprites                         */                        void add_frame(std::string);                private:                        std::vector<Object> frames;        };        /**         * Collision detecting function.         * @param a Object to check for collision with b         * @param b Object to check for collision with a          * @return Returns true if a and b has collided, otherwise, it returns false.         */        bool collision(Sprite& a, Sprite& b);        Zen::Coord ellipse(int, int, int);}#endif#ifndef _ztext_hpp_#define _ztext_hpp_#include <string>#include "SDL.h"#include "color.hpp"namespace Zen {        class Text {                public:                        Text(std::string, std::string, Color, int);                        std::string get_text();                        std::string get_font();                        SDL_Color get_color();                        int get_size();                private:                        std::string text;                        std::string font;                        Color color;                        int size;        };}#endif#ifndef _zengine_hpp_#define _zengine_hpp_#include <color.hpp>#include <coord.hpp>#include <display.hpp>#include <event.hpp>#include <exception.hpp>#include <input.hpp>#include <object.hpp>#include <object_container.hpp>#include <platform.hpp>#include <rect.hpp>#include <sprite.hpp>#include <text.hpp>#include <sigc++/sigc++.h>#endif#include "color.hpp"namespace Zen {        Color::Color(int rr, int gg, int bb) : r(rr), g(gg), b(bb)        {        }}#include <cstdlib>#include <vector>#include "SDL.h"#include "SDL_ttf.h"#include "display.hpp"#include "object.hpp"#include "exception.hpp"namespace Zen {        Display::Display(int w, int h, int flags)        {                if(!SDL_WasInit(SDL_INIT_VIDEO) && SDL_Init(SDL_INIT_VIDEO) == -1)                        throw Exception("Unable to init SDL: ", SDL_GetError());                if(!TTF_WasInit() && TTF_Init() == -1)                        throw Exception("Unable to init SDL_ttf: ", TTF_GetError());                std::atexit(SDL_Quit);                std::atexit(TTF_Quit),                screen.init(w, h, flags);        }        void Display::register_object(Object* oo)        {                objects.push_back(oo);        }        void Display::show()        {                std::vector<Object*>::iterator iter(objects.begin());                 while(iter != objects.end()) {                        (*iter)->blit_to(&screen);                        ++iter;                }                screen.flip();        }        void Display::clear(Color cl)        {                screen.fill_rect(cl);        }        int Display::width()        {                screen.width();        }        int Display::height()        {                return screen.height();        }}#include "event.hpp"namespace Zen {        Event::Event(SDL_Event& ee) : event(ee)        {        }        int Event::type()        {                return event.type;        }        char Event::keydown()        {                return static_cast<char>(event.key.keysym.sym);        }        unsigned char* Event::keystate()        {                return SDL_GetKeyState(0);        }        bool Event::mouse_motion()        {                return event.type == SDL_MOUSEMOTION;        }        bool Event::mouse_clicked()        {                return event.type == SDL_MOUSEBUTTONDOWN;        }        int Event::mouse_pos_x()        {                return event.button.x;        }        int Event::mouse_pos_y()        {                return event.button.y;        }}#include <string>#include "exception.hpp"namespace Zen {        Exception::Exception(std::string desc, std::string err)        {                message = desc;                message += err;        }        const std::string& Exception::what() const        {                return message;        }}#include <cstdlib>#include <sigc++/sigc++.h>#include "SDL.h"#include "input.hpp"#include "exception.hpp"namespace Zen {        Input::Input() { }        void Input::read(int type)        {                bool successful_read = false;                if(type == POLL)                        if(SDL_PollEvent(&event))                                successful_read = true;                if(type == WAIT)                        if(SDL_WaitEvent(&event))                                successful_read = true;                if(successful_read) {                        if(event.type == SDL_QUIT)                                std::exit(0);                        Event ee(event);                        signal_event.emit(ee);                }        }}#include "object.hpp"#include "object_container.hpp"namespace Zen {        template<template<class> class T>        T<Zen::Object>& ObjectContainer<T>::array()        {                return container;        }        template<template<class> class T>        void ObjectContainer<T>::collision(Zen::Object& oo)        {                typename T<Zen::Object>::iterator it;                for(it = container.begin(); it != container.end(); ++it)                        if(collision(*it, oo))                                signal_collision.emit(*it, oo);        }}#include <string>#include "SDL.h"#include "SDL_image.h"#include "SDL_ttf.h"#include "object.hpp"#include "screen.hpp" #include "text.hpp"#include "color.hpp"#include "exception.hpp"#include "rect.hpp"#include "coord.hpp"namespace Zen {        Object::Object()        {                set_coordinate(Coord(0, 0));        }        Object::Object(std::string img)         {                set_coordinate(Coord(0, 0));                load_image(img);                set_rect(Rect(0, 0, surface->w, surface->h));        }        Object::~Object()        {                SDL_FreeSurface(surface);        }        void Object::load_image(std::string ss)        {                surface = SDL_DisplayFormat(IMG_Load(ss.c_str()));                if(!surface)                        throw Exception("Unable to load image: ", SDL_GetError());        }        void Object::load_text(Text text)        {                TTF_Font* font = TTF_OpenFont(text.get_font().c_str(), text.get_size());                if(!font)                        throw Exception("Unable to open font: ", TTF_GetError());                surface = TTF_RenderText_Solid(font, text.get_text().c_str(),                                 text.get_color());                if(!surface)                        throw Exception("Unable to load text: ", TTF_GetError());        }        void Object::set_rect(Rect rect)        {                source.x(rect.x());                source.y(rect.y());                source.w(rect.w());                source.h(rect.h());        }        Rect Object::get_rect()        {                return source;        }        void Object::set_coordinate(Coord cc)        {                coord = cc;        }        Coord Object::get_coordinate()        {                return coord;        }        void Object::set_colorkey(Color cc)        {                SDL_SetColorKey(surface, SDL_SRCCOLORKEY,                                 SDL_MapRGB(surface->format, cc.r, cc.g, cc.b));        }        void Object::blit_to(Object* target)        {                if(!surface || !target->surface)                        throw Exception("Target or destination surface not set");                SDL_Rect dd = { coord.x(), coord.y(), 0, 0 };                if(!source.value_set()) {                        SDL_Rect ss = { source.x(), source.y(), source.w(), source.h() };                        SDL_BlitSurface(surface, &ss, target->surface, &dd);                }                else                        SDL_BlitSurface(surface, 0, target->surface, &dd);        }        // error        void Object::blit_to_self(Object* target)        {                /*                static bool surface_created = false;                if(!surface_created) {                        SDL_Surface* screen = SDL_GetVideoSurface();                        surface = SDL_CreateRGBSurface(screen->flags,                                                                                 screen->w,                                                                                 screen->h,                                                                                 screen->format->BitsPerPixel,                                                                                 screen->format->Rmask,                                                                                 screen->format->Gmask,                                                                                 screen->format->Bmask,                                                                                 screen->format->Amask);                        if(!surface)                                throw Exception("Error while creating surface: ",                                                 SDL_GetError());                        surface_created = true;                }                target->blit_to(this);                */                SDL_BlitSurface(target->surface, 0, surface, 0);        }        int Object::width()        {                return surface->w;        }        int Object::height()        {                 return surface->h;        }}#include <iostream>#include <string>#include "platform.hpp"#include "coord.hpp"#include "rect.hpp"namespace Zen {        namespace View {                void Platform::set_background_layer(std::string bl)                {                        background_layer.load_image(bl);                        background_layer.set_coordinate(Coord(0, 0));                }                void Platform::set_layer1(std::string l1)                {                        layer1.load_image(l1);                        layer1.set_coordinate(Coord(0, 0));                }                void Platform::set_layer2(std::string l2)                {                        layer2.load_image(l2);                        layer2.set_coordinate(Coord(0, 0));                }                void Platform::set_layer3(std::string l3)                {                        layer3.load_image(l3);                        layer3.set_coordinate(Coord(0, 0));                }                void Platform::set_base_speed(int bs)                {                        base_speed = bs;                }                void Platform::scroll(Direction dir)                {                        switch(dir) {                                case LEFT:      {                                                                Rect l1r = layer1.get_rect();                                                                l1r.x(l1r.x() + base_speed);                                                                 layer1.set_rect(l1r);                                                                Rect l2r = layer2.get_rect();                                                                l2r.x(l2r.x() + base_speed * 2);                                                                layer2.set_rect(l2r);                                                                Rect l3r = layer3.get_rect();                                                                l3r.x(l3r.x() + base_speed * 2);                                                                layer3.set_rect(l3r);                                                        }                                case RIGHT: {                                                                Coord l1c = layer1.get_coordinate();                                                                l1c.x(l1c.x() + base_speed);                                                                layer1.set_coordinate(l1c);                                                                Coord l2c = layer2.get_coordinate();                                                                l2c.x(l2c.x() + base_speed * 2);                                                                layer2.set_coordinate(l2c);                                                                Coord l3c = layer3.get_coordinate();                                                                l3c.x(l3c.x() + base_speed * 2);                                                                layer3.set_coordinate(l3c);                                                        }                        }                }                void Platform::blit_to(Object* target)                {                        blit_to_self(&background_layer);                        blit_to_self(&layer1);                        blit_to_self(&layer2);                        blit_to_self(&layer3);                        Object::blit_to(target);                }        }  // namespace View} // namespace Zen#include "SDL.h"#include "exception.hpp"#include "screen.hpp"#include "color.hpp"namespace Zen {        void Screen::init(int w, int h, int flags)        {                surface = SDL_SetVideoMode(w, h, 0, flags);                if(!surface)                         throw Exception("Unable to init SDL: ", SDL_GetError());        }        void Screen::flip()        {                SDL_Flip(surface);        }        void Screen::fill_rect(Color cl)        {                SDL_FillRect(surface, 0, SDL_MapRGB(surface->format, cl.r, cl.g, cl.b));        }}#include <string>#include <cmath>#include "sprite.hpp"namespace Zen {        void Sprite::add_frame(std::string ff)        {        }    bool collision(Sprite* ll, Sprite* rr)    {        Coord llc = ll->get_coordinate();        Coord rrc = rr->get_coordinate();        if((llc.x() <= rrc.x() && llc.x() + ll->width() >= rrc.x()) ||                                 (rrc.x() <= llc.x() && rrc.x() + rr->width() >= llc.x()))            if((llc.y() <= rrc.y() &&  llc.y() + ll->height() >= rrc.y()) ||                                         (rrc.y() <= llc.y() && rrc.y() + rr->height() >= llc.y()))                return true;        return false;    }    Zen::Coord ellipse(int a, int b, int speed)    {        static int current_degree = 0;        static const float de2grad = M_PI/180;        const float deingrad = current_degree*de2grad;        if(current_degree >= 360)            current_degree = 0;        Zen::Coord cc(static_cast<int>(std::cos(deingrad)*a),                      static_cast<int>(std::sin(deingrad)*b));        current_degree += speed;        return cc;    }}#include <string>#include "SDL.h"#include "text.hpp"#include "color.hpp"#include "exception.hpp"namespace Zen {        Text::Text(std::string txt, std::string fnt, Color clr, int sz) :                text(txt),                font(fnt),                color(clr),                size(sz)        {        }        std::string Text::get_text()        {                return text;        }        std::string Text::get_font()        {                return font;        }        SDL_Color Text::get_color()        {                SDL_Color cc = {color.r, color.g, color.b};                return cc;        }        int Text::get_size()        {                return size;        }}