Dino Drokan avatar Dino Drokan committed 8870f72 Merge

Merge

Comments (0)

Files changed (17)

 		</Build>
 		<Compiler>
 			<Add option="-Wall" />
+			<Add option="-DHAVE_SDL_MIXER=1" />
 		</Compiler>
 		<Linker>
 			<Add library="mingw32" />
 			<Add library="OpenGL32" />
 			<Add library="glu32" />
 			<Add library="SDL_image" />
+			<Add library="SDL_mixer" />
 		</Linker>
 		<Unit filename="asteroid.cpp" />
 		<Unit filename="asteroid.h" />
+		<Unit filename="audiomgr.cpp" />
+		<Unit filename="audiomgr.h" />
 		<Unit filename="blueenemyship.cpp" />
 		<Unit filename="blueenemyship.h" />
 		<Unit filename="bullet.cpp" />
 		<Unit filename="redenemyship.h" />
 		<Unit filename="texture.cpp" />
 		<Unit filename="texture.h" />
+		<Unit filename="texture_manager.cpp" />
+		<Unit filename="texture_manager.h" />
 		<Unit filename="yellowenemyship.cpp" />
 		<Unit filename="yellowenemyship.h" />
 		<Extensions>

Pucaljka.xcodeproj/project.pbxproj

 		7F9B660D1719986C004DC616 /* playership.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7F9B660A1719986C004DC616 /* playership.cpp */; };
 		7F9B66101719987B004DC616 /* enemyship01.png in Resources */ = {isa = PBXBuildFile; fileRef = 7F9B660E1719987B004DC616 /* enemyship01.png */; };
 		7F9B66111719987B004DC616 /* playership.png in Resources */ = {isa = PBXBuildFile; fileRef = 7F9B660F1719987B004DC616 /* playership.png */; };
+		7FE4C441175902BA008D87FC /* texture_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FE4C43F175902BA008D87FC /* texture_manager.cpp */; };
 		7FFE2EB81722D53E0059ECD6 /* yellowenemyship.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FFE2EB61722D53E0059ECD6 /* yellowenemyship.cpp */; };
 		7FFE2EBA1722DE0B0059ECD6 /* enemyship02.png in Resources */ = {isa = PBXBuildFile; fileRef = 7FFE2EB91722DE0B0059ECD6 /* enemyship02.png */; };
 		AF1D1E8F1477E6D400C95550 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AF1D1E8E1477E6D400C95550 /* Cocoa.framework */; };
 		7F9B660B1719986C004DC616 /* playership.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = playership.h; sourceTree = SOURCE_ROOT; };
 		7F9B660E1719987B004DC616 /* enemyship01.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = enemyship01.png; sourceTree = "<group>"; };
 		7F9B660F1719987B004DC616 /* playership.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = playership.png; sourceTree = "<group>"; };
+		7FE4C43F175902BA008D87FC /* texture_manager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = texture_manager.cpp; sourceTree = SOURCE_ROOT; };
+		7FE4C440175902BA008D87FC /* texture_manager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = texture_manager.h; sourceTree = SOURCE_ROOT; };
 		7FFE2EB61722D53E0059ECD6 /* yellowenemyship.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = yellowenemyship.cpp; sourceTree = SOURCE_ROOT; };
 		7FFE2EB71722D53E0059ECD6 /* yellowenemyship.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = yellowenemyship.h; sourceTree = SOURCE_ROOT; };
 		7FFE2EB91722DE0B0059ECD6 /* enemyship02.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = enemyship02.png; sourceTree = "<group>"; };
 				7F225DD91710459C00D00DBE /* pucaljka.cpp */,
 				7F1510C2172C2F69001DEF50 /* redenemyship.cpp */,
 				AF1D1EB31477E73E00C95550 /* texture.cpp */,
+				7FE4C43F175902BA008D87FC /* texture_manager.cpp */,
 				7FFE2EB61722D53E0059ECD6 /* yellowenemyship.cpp */,
 				7F2FD0981750F44B00643CB5 /* asteroid.h */,
 				7F1510BC172C2A19001DEF50 /* blueenemyship.h */,
 				7F1510C3172C2F69001DEF50 /* redenemyship.h */,
 				AF1D1EB11477E73E00C95550 /* SDLMain.h */,
 				AF1D1EB41477E73E00C95550 /* texture.h */,
+				7FE4C440175902BA008D87FC /* texture_manager.h */,
 				7FFE2EB71722D53E0059ECD6 /* yellowenemyship.h */,
 				AF1D1EB21477E73E00C95550 /* SDLMain.m */,
 				AF1D1E951477E6D400C95550 /* Supporting Files */,
 				7F74A6DF1747BDCF00CE1680 /* bullet.cpp in Sources */,
 				7F2FD0991750F44B00643CB5 /* asteroid.cpp in Sources */,
 				7F2FD0A01750FFB500643CB5 /* audiomgr.cpp in Sources */,
+				7FE4C441175902BA008D87FC /* texture_manager.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 #include "asteroid.h"
 #include "texture.h"
+#include "texture_manager.h"
 
 Asteroid::Asteroid(const double argX,
                    const double argY,
-                   const double argVY) : texture(new Texture("asteroid.png")),  x(argX), y(argY), vy(argVY)
+                   const double argVY) : texture(TextureManager::getInstance()->getTexture("asteroid.png")),
+                                         x(argX), y(argY), vy(argVY)
 {
+    this->texture->retain();
 }
 Asteroid::~Asteroid()
 {
+    this->texture->release();
 }
 void
 Asteroid::paintSelf()
 #else
 #include <SDL_mixer/SDL_mixer.h>
 #endif
-#include "util.h"
 
 
 
 #include "texture.h"
 #include "playership.h"
 #include "gm_level.h"
+#include "texture_manager.h"
 
-Bullet::Bullet( double argX, double argY) : x(argX), y(argY), texture(new Texture("bullet.png"))
+Bullet::Bullet( double argX, double argY) : texture(TextureManager::getInstance()->getTexture("bullet.png")),
+                                            x(argX), y(argY)
 {
+    this->texture->retain();
 }
-
+Bullet::~Bullet()
+{
+    this->texture->release();
+}
 void
 Bullet::updateSelf(const double timeDelta)
 {
     for(std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
     {
         GameObject * currentObject = *it;
-        
+
         if(this == currentObject)
             continue;
-        
+
         EnemyShip * currentEnemyShip = dynamic_cast<EnemyShip*>(currentObject);
         if(currentEnemyShip) // if(currentEnemyShip != NULL)
         {
 
     glEnd();
     glPopMatrix();
-    
+
 }
     public:
     Bullet( const double x,
             const double y);
-
+    virtual ~Bullet();
 
     virtual void paintSelf(void);
 
 #include "texture.h"
 #include "gm_level.h"
 #include "gm_credits.h"
+#include "gm_mainmenu.h"
 #include "gamemode.h"
+#include "texture_manager.h"
 
 extern bool gRunning;
 
 Button::Button(const std::string& textureFile,
                const double argX,
-               const double argY) : texture(new Texture(textureFile)), x(argX), y(argY)
+               const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                    x(argX), y(argY)
 {
+    this->texture->retain();
 }
 Button::~Button()
 {
-    delete this->texture;
+    this->texture->release();
 }
 
 void Button::paintSelf()
     return false;
 }
 
-///////////////////////////////
-
+////////////////////////////////
 bool ExitButton::handleClick()
 {
-    gRunning = 0;
+    gRunning = false;
     return true;
 }
 ///////////////////////////////
     gGame->switchGameMode(new GM_Credits());
     return true;
 }
-
+////////////////////////////////
+bool BackToMainMenuButton::handleClick()
+{
+    gGame->switchGameMode(new GM_MainMenu());
+    return true;
+}
     bool handleClick();
 };
 
+class BackToMainMenuButton : public Button
+{
+    public:
+    BackToMainMenuButton(const std::string& textureFile,
+                         const double x,
+                         const double y) :
+                         Button(textureFile, x, y)
+    {
+    }
+    bool handleClick();
+};
+
 #endif
 #include "EnemyShip.h"
 #include "texture.h"
+#include "texture_manager.h"
 #include <math.h>
 
 bool gDrawCollisionRects;
 
 EnemyShip::EnemyShip(const std::string& textureFile,
                const double argX,
-               const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0)
+               const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                    x(argX), y(argY), angle(0)
 {
+    this->texture->retain();
 }
-
 EnemyShip::~EnemyShip()
 {
+    this->texture->release();
 }
 
 void
 {
     if(this->getShouldDie())
         glColor4f(1, 0, 0, 1);
-    
+
     this->texture->bind();
     glEnable(GL_TEXTURE_2D);
 
 
 
     glColor4f(1, 1, 1, 1);
-    
+
     if(gDrawCollisionRects)
     {
         glDisable(GL_TEXTURE_2D);
 extern bool gRunning;
 extern SDL_Surface * gScreen;
 
-
 GM_Credits::GM_Credits()
 {
     Layer *layer(new Layer("background.png", gScreen->w / 2,gScreen->h / 2));
     Layer *layer2(new Layer("credits.png", gScreen->w / 2, gScreen->h * 1.5));
     this->layers.push_back(layer2);
 
-    Button *exitButton(new ExitButton("exitbutton.png", 130, 335));
+    Button *exitButton(new BackToMainMenuButton("mainmenubutton.png", 130, 335));
     this->buttons.push_back(exitButton);
 
 
 #include "layer.h"
 #include "texture.h"
+#include "texture_manager.h"
 
 
 
 Layer::Layer(const std::string& textureFile,
                        const int argX,
-                       const int argY) : texture(new Texture(textureFile)), x(argX), y(argY)
-                       {
-                       }
+                       const int argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                         x(argX), y(argY)
+{
+    this->texture->retain();
+}
 Layer::~Layer()
 {
-    delete this->texture;
+    this->texture->release();
 }
 void
 Layer::paintSelf()
     private:
     Texture * texture;
     double x, y;
-
 };
 
 
 #include "gm_level.h"
 #include "gamemode.h"
 #include "audiomgr.h"
+#include "texture_manager.h"
 
 static const double playerShipAngleV = 180;
 static const double playerShipYV = 380;
 
 PlayerShip::PlayerShip(const std::string& textureFile,
                        const double argX,
-                       const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
+                       const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                        x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
 {
+    this->texture->retain();
     shootSound = AudioMgr::getInstance()->loadSound("gun.wav");
 }
 PlayerShip::~PlayerShip()
 {
-    delete this->texture;
+    this->texture->release();
     AudioMgr::getInstance()->unloadSound(shootSound);
 }
 void
     angle += angleV * timeDelta;
     y += yv * timeDelta;
     timeUntilNextFiring -= timeDelta;
-    
+
     static bool prevJoyState = 0;
     bool currentJoyState = (gJoystick && SDL_JoystickGetButton(gJoystick, 0));
-    
+
     if(SDL_GetKeyState(NULL)[SDLK_SPACE] ||
        currentJoyState)
     {
             timeUntilNextFiring = 0;
         fire();
     }
-    
+
     if(gJoystick)
     {
         Sint16 axisYInt = SDL_JoystickGetAxis(gJoystick, 1);
         double axisY = (axisYInt / 32767.);
-        
+
         y += axisY * playerShipYV * timeDelta;
     }
-    
+
     if(y < -240) y = -240;
     if(y > 240) y = 240;
     prevJoyState = currentJoyState;
         case SDLK_DOWN:
         yv =  playerShipYV;
         break;
-            
+
         case SDLK_SPACE:
         timeUntilNextFiring = 0;
         break;
 {
     if(this->timeUntilNextFiring > 0)
         return;
-    
+
     Bullet * b = new Bullet(this->x, this->y);
     GM_Level * currentLevel = (GM_Level*)gGame;
-    
+
     AudioMgr::getInstance()->playSound(shootSound);
     currentLevel->addGameObject(b);
-    
+
     this->timeUntilNextFiring = this->getFiringRate();
 }
 
 #include <OpenGL/glu.h>
 #endif
 #include "texture.h"
+#include "texture_manager.h"
 
-Texture::Texture(const std::string& textureFile)
+Texture::Texture(const std::string& textureFile) : refCount(0), textureFile(textureFile)
 {
-    this->textureFile = textureFile;
     this->_loadTexture();
 }
 
 {
     glBindTexture(GL_TEXTURE_2D, this->textureId);
 }
+
+void Texture::release()
+{
+    this->refCount--;
+    if(this->refCount >= 1)
+        return;
+    
+    TextureManager::getInstance()->unloadTexture(this->getFilename());
+}
 
     int getW() const { return this->w; }
     int getH() const { return this->h; }
+    const std::string& getFilename() const { return this->textureFile; }
 
+    void retain() { this->refCount++; }
+    void release();
+    
     private:
     std::string textureFile;
     GLuint textureId;
     int w, h;
+    int refCount;
 
     void _loadTexture();
 };

texture_manager.cpp

+#include "texture_manager.h"
+#include "texture.h"
+#include <stdio.h>
+
+TextureManager* TextureManager::instance;
+
+TextureManager::TextureManager()
+{
+    bLoaded = false;
+}
+
+TextureManager::~TextureManager()
+{
+
+}
+
+TextureManager* TextureManager::getInstance()
+{
+    if(!instance)
+        instance = new TextureManager();
+    return instance;
+}
+
+void
+TextureManager::destroyInstance()
+{
+    delete instance;
+    instance = NULL;
+}
+
+bool
+TextureManager::isLoaded()
+{
+    return this->bLoaded;
+}
+
+Texture* TextureManager::getTexture(std::string filename)
+{
+    if(textures.count(filename) == 0)
+    {
+        Texture * texture = new Texture(filename);
+        std::map<std::string, Texture*>::iterator it = textures.end();
+        textures.insert(it, std::pair<std::string, Texture*>(filename, texture));
+        return texture;
+
+    }
+    else
+    {
+        return textures.find(filename)->second;
+    }
+
+}
+void TextureManager::unloadTexture(const std::string &filename)
+{
+    std::map<std::string, Texture*>::iterator it = textures.find(filename);
+    if(it == textures.end())
+        return; // already unloaded
+
+    printf("Unloading %s\n", filename.c_str());
+    delete it->second;
+    textures.erase(it);
+}

texture_manager.h

+#ifndef PUCALJKA_TEXTURE_MANAGER_H
+#define PUCALJKA_TEXTURE_MANAGER_H
+
+#include <map>
+#include <string>
+class Texture;
+
+
+class TextureManager
+{
+    public:
+    TextureManager();
+    ~TextureManager();
+
+    static TextureManager* getInstance();
+    Texture* getTexture(std::string filename);
+    static void destroyInstance();
+    bool isLoaded();
+    void unloadTexture(const std::string& filename);
+
+    private:
+    std::map<std::string, Texture*> textures;
+    static TextureManager* instance;
+    bool bLoaded;
+
+
+
+};
+
+
+#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.