From 6dfcfbd4a612230f2037cf891dd98520cb80c997 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Wed, 8 Jul 2009 13:31:12 -0600 Subject: [PATCH] new classes: resource, tilemap, animation --- share/animation/AlienWarrior.json | 184 +++++++++++++ share/animation/BigExplosion.json | 26 ++ share/animation/Bonuses.json | 134 ++++++++++ share/animation/Bullets.json | 65 +++++ share/animation/Effects.json | 105 ++++++++ share/animation/Heroine.json | 253 ++++++++++++++++++ share/animation/Jetbot.json | 179 +++++++++++++ share/animation/RobotTrooper.json | 203 ++++++++++++++ share/animations/AlienWarrior.xml | 73 ----- share/animations/BigExplosion.xml | 1 - share/animations/Bonuses.xml | 1 - share/animations/Bullets.xml | 1 - share/animations/Effects.xml | 1 - share/animations/Heroine.xml | 1 - share/animations/Jetbot.xml | 1 - share/animations/RobotTrooper.xml | 1 - share/{scenes => scene}/Test.xml | 0 share/{textures => texture}/AlienWarrior.data | 0 .../{textures => texture}/AlienWarrior.plist | 0 share/{textures => texture}/AlienWarrior.png | Bin .../{textures => texture}/BackgroundFar.data | 0 .../{textures => texture}/BackgroundFar.plist | 0 share/{textures => texture}/BackgroundFar.png | Bin .../{textures => texture}/BackgroundNear.data | 0 .../BackgroundNear.plist | 0 .../{textures => texture}/BackgroundNear.png | Bin share/{textures => texture}/BigExplosion.data | 0 .../{textures => texture}/BigExplosion.plist | 0 share/{textures => texture}/BigExplosion.png | Bin share/{textures => texture}/Bonuses.data | 0 share/{textures => texture}/Bonuses.plist | 0 share/{textures => texture}/Bonuses.png | Bin share/{textures => texture}/Building.data | 0 share/{textures => texture}/Building.plist | 0 share/{textures => texture}/Building.png | Bin share/{textures => texture}/Default.plist | 0 share/{textures => texture}/Font.data | 0 share/{textures => texture}/Font.plist | 0 share/{textures => texture}/Font.png | Bin share/{textures => texture}/Heroine.data | 0 share/{textures => texture}/Heroine.plist | 0 share/{textures => texture}/Heroine.png | Bin share/{textures => texture}/Jetbot.data | 0 share/{textures => texture}/Jetbot.plist | 0 share/{textures => texture}/Jetbot.png | Bin share/{textures => texture}/Particles.data | 0 share/{textures => texture}/Particles.plist | 0 share/{textures => texture}/Particles.png | Bin share/{textures => texture}/RobotTrooper.data | 0 .../{textures => texture}/RobotTrooper.plist | 0 share/{textures => texture}/RobotTrooper.png | Bin share/{textures => texture}/Scenery.data | 0 share/{textures => texture}/Scenery.plist | 0 share/{textures => texture}/Scenery.png | Bin share/{textures => texture}/StatusBars.data | 0 share/{textures => texture}/StatusBars.plist | 0 share/{textures => texture}/StatusBars.png | Bin share/{textures => texture}/TowerBlock1.data | 0 share/{textures => texture}/TowerBlock1.plist | 0 share/{textures => texture}/TowerBlock1.png | Bin share/{textures => texture}/Trees.data | 0 share/{textures => texture}/Trees.plist | 0 share/{textures => texture}/Trees.png | Bin src/Makefile.am | 9 +- src/animation.cc | 50 ++++ src/animation.hh | 64 +++++ src/deserializer.cc | 2 +- src/deserializer.hh | 4 +- src/resource.cc | 88 ++++++ src/resource.hh | 94 +++++++ src/serializer.cc | 8 +- src/serializer.hh | 4 +- src/settings.cc | 2 +- src/singleton.hh | 20 +- src/texture.cc | 83 +++--- src/texture.hh | 26 +- src/tilemap.hh | 157 +++++++++++ 77 files changed, 1687 insertions(+), 153 deletions(-) create mode 100644 share/animation/AlienWarrior.json create mode 100644 share/animation/BigExplosion.json create mode 100644 share/animation/Bonuses.json create mode 100644 share/animation/Bullets.json create mode 100644 share/animation/Effects.json create mode 100644 share/animation/Heroine.json create mode 100644 share/animation/Jetbot.json create mode 100644 share/animation/RobotTrooper.json delete mode 100644 share/animations/AlienWarrior.xml delete mode 100644 share/animations/BigExplosion.xml delete mode 100644 share/animations/Bonuses.xml delete mode 100644 share/animations/Bullets.xml delete mode 100644 share/animations/Effects.xml delete mode 100644 share/animations/Heroine.xml delete mode 100644 share/animations/Jetbot.xml delete mode 100644 share/animations/RobotTrooper.xml rename share/{scenes => scene}/Test.xml (100%) rename share/{textures => texture}/AlienWarrior.data (100%) rename share/{textures => texture}/AlienWarrior.plist (100%) rename share/{textures => texture}/AlienWarrior.png (100%) rename share/{textures => texture}/BackgroundFar.data (100%) rename share/{textures => texture}/BackgroundFar.plist (100%) rename share/{textures => texture}/BackgroundFar.png (100%) rename share/{textures => texture}/BackgroundNear.data (100%) rename share/{textures => texture}/BackgroundNear.plist (100%) rename share/{textures => texture}/BackgroundNear.png (100%) rename share/{textures => texture}/BigExplosion.data (100%) rename share/{textures => texture}/BigExplosion.plist (100%) rename share/{textures => texture}/BigExplosion.png (100%) rename share/{textures => texture}/Bonuses.data (100%) rename share/{textures => texture}/Bonuses.plist (100%) rename share/{textures => texture}/Bonuses.png (100%) rename share/{textures => texture}/Building.data (100%) rename share/{textures => texture}/Building.plist (100%) rename share/{textures => texture}/Building.png (100%) rename share/{textures => texture}/Default.plist (100%) rename share/{textures => texture}/Font.data (100%) rename share/{textures => texture}/Font.plist (100%) rename share/{textures => texture}/Font.png (100%) rename share/{textures => texture}/Heroine.data (100%) rename share/{textures => texture}/Heroine.plist (100%) rename share/{textures => texture}/Heroine.png (100%) rename share/{textures => texture}/Jetbot.data (100%) rename share/{textures => texture}/Jetbot.plist (100%) rename share/{textures => texture}/Jetbot.png (100%) rename share/{textures => texture}/Particles.data (100%) rename share/{textures => texture}/Particles.plist (100%) rename share/{textures => texture}/Particles.png (100%) rename share/{textures => texture}/RobotTrooper.data (100%) rename share/{textures => texture}/RobotTrooper.plist (100%) rename share/{textures => texture}/RobotTrooper.png (100%) rename share/{textures => texture}/Scenery.data (100%) rename share/{textures => texture}/Scenery.plist (100%) rename share/{textures => texture}/Scenery.png (100%) rename share/{textures => texture}/StatusBars.data (100%) rename share/{textures => texture}/StatusBars.plist (100%) rename share/{textures => texture}/StatusBars.png (100%) rename share/{textures => texture}/TowerBlock1.data (100%) rename share/{textures => texture}/TowerBlock1.plist (100%) rename share/{textures => texture}/TowerBlock1.png (100%) rename share/{textures => texture}/Trees.data (100%) rename share/{textures => texture}/Trees.plist (100%) rename share/{textures => texture}/Trees.png (100%) create mode 100644 src/animation.cc create mode 100644 src/animation.hh create mode 100644 src/resource.cc create mode 100644 src/resource.hh create mode 100644 src/tilemap.hh diff --git a/share/animation/AlienWarrior.json b/share/animation/AlienWarrior.json new file mode 100644 index 0000000..15d28cf --- /dev/null +++ b/share/animation/AlienWarrior.json @@ -0,0 +1,184 @@ +{ + "Stand": { + "type": "Stand", + "delay": 1, + "frames": [ + { + "index": 0, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + }, + { + "index": 2, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + } + ] + }, + "Run": { + "type": "Run", + "delay": 0.25, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + }, + { + "index": 5 + }, + { + "index": 6 + } + ] + }, + "StopRunning": { + "type": "Stand", + "next": "Stand", + "delay": 10, + "loop": true, + "frames": [ + { + "index": 7 + } + ] + }, + "Jump": { + "type": "Jump", + "next": "FlyDiagonallyUp", + "delay": 0.25, + "frames": [ + { + "index": 17 + } + ] + }, + "Land": { + "type": "Stand", + "next": "Stand", + "delay": 1, + "loop": true, + "frames": [ + { + "index": 13, + "duration": 0.25 + } + ] + }, + "FlyDiagonallyUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 18 + } + ] + }, + "FlyDiagonallyDown": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 19 + } + ] + }, + "FlyStraightUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 19 + } + ] + }, + "FlyHorizontally": { + "type": "Fly", + "frames": [ + { + "index": 3 + } + ] + }, + "Punch": { + "type": "Attack", + "delay": 0.2, + "frames": [ + { + "index": 8 + }, + { + "index": 9 + } + ] + }, + "StartFiring": { + "type": "Attack", + "next": "Fire", + "delay": 0.1, + "frames": [ + { + "index": 0 + }, + { + "index": 8 + } + ] + }, + "Fire": { + "type": "Attack", + "delay": 1, + "frames": [ + { + "index": 10, + "duration": 0.2 + }, + { + "index": 11, + "duration": 0.2 + } + ] + }, + "StopFiring": { + "type": "Attack", + "next": "Stand", + "delay": 0.1, + "frames": [ + { + "index": 8 + }, + { + "index": 0 + } + ] + }, + "KnockedDown": { + "type": "Hit", + "delay": 0.1, + "loop": true, + "frames": [ + { + "index": 16 + }, + { + "index": 15 + } + ] + }, + "Flattened": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 13 + } + ] + } +} diff --git a/share/animation/BigExplosion.json b/share/animation/BigExplosion.json new file mode 100644 index 0000000..a0cb719 --- /dev/null +++ b/share/animation/BigExplosion.json @@ -0,0 +1,26 @@ +{ + "BigExplosion": { + "delay": 0.1, + "loop": true, + "frames": [ + { + "index": 0 + }, + { + "index": 1 + }, + { + "index": 2 + }, + { + "index": 3 + }, + { + "index": 4 + }, + { + "index": 5 + } + ] + } +} diff --git a/share/animation/Bonuses.json b/share/animation/Bonuses.json new file mode 100644 index 0000000..88cacad --- /dev/null +++ b/share/animation/Bonuses.json @@ -0,0 +1,134 @@ +{ + "SmallHealth": { + "delay": 0.1, + "frames": [ + { + "index": 0 + }, + { + "index": 1 + }, + { + "index": 2 + }, + { + "index": 1 + } + ] + }, + "LargeHealth": { + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + }, + { + "index": 5 + }, + { + "index": 4 + } + ] + }, + "SmallRedPotion": { + "delay": 0.1, + "frames": [ + { + "index": 6 + }, + { + "index": 7 + }, + { + "index": 8 + }, + { + "index": 7 + } + ] + }, + "SmallBluePotion": { + "delay": 0.1, + "frames": [ + { + "index": 9 + }, + { + "index": 10 + }, + { + "index": 11 + }, + { + "index": 10 + } + ] + }, + "FirstAidKit": { + "delay": 1, + "frames": [ + { + "index": 12 + } + ] + }, + "AlienFirstAidKit": { + "delay": 1, + "frames": [ + { + "index": 13 + } + ] + }, + "Cake": { + "delay": 1, + "frames": [ + { + "index": 14 + } + ] + }, + "FizzyDrink": { + "delay": 1, + "frames": [ + { + "index": 15 + } + ] + }, + "Cheese": { + "delay": 1, + "frames": [ + { + "index": 16 + } + ] + }, + "Chocolate": { + "delay": 1, + "frames": [ + { + "index": 17 + } + ] + }, + "MilkBottle": { + "delay": 1, + "frames": [ + { + "index": 18 + } + ] + }, + "OneSadCookie": { + "delay": 1, + "frames": [ + { + "index": 19 + } + ] + } +} diff --git a/share/animation/Bullets.json b/share/animation/Bullets.json new file mode 100644 index 0000000..caca7af --- /dev/null +++ b/share/animation/Bullets.json @@ -0,0 +1,65 @@ +{ + "Punch": { + "delay": 1, + "frames": [ + { + "index": 31 + } + ] + }, + "SmallBullet": { + "delay": 0.05, + "frames": [ + { + "index": 16 + }, + { + "index": 17 + } + ] + }, + "MediumBullet": { + "delay": 0.05, + "frames": [ + { + "index": 17 + }, + { + "index": 18 + } + ] + }, + "SmallFireball": { + "delay": 0.05, + "frames": [ + { + "index": 0 + }, + { + "index": 1 + } + ] + }, + "MediumFireball": { + "delay": 0.05, + "frames": [ + { + "index": 2 + }, + { + "index": 1 + } + ] + }, + "LargeFireball": { + "delay": 0.05, + "frames": [ + { + "index": 21 + }, + { + "index": 2 + } + ] + } +} diff --git a/share/animation/Effects.json b/share/animation/Effects.json new file mode 100644 index 0000000..35e0464 --- /dev/null +++ b/share/animation/Effects.json @@ -0,0 +1,105 @@ +{ + "Fireball": { + "delay": 0.1, + "frames": [ + { + "index": 0 + }, + { + "index": 1 + }, + { + "index": 2 + }, + { + "index": 1 + }, + { + "index": 0 + } + ] + }, + "Smoke": { + "delay": 0.05, + "loop": true, + "frames": [ + { + "index": 4 + }, + { + "index": 5 + }, + { + "index": 6 + } + ] + }, + "GreenDiamond": { + "delay": 0.05, + "frames": [ + { + "index": 7 + }, + { + "index": 8 + }, + { + "index": 9 + }, + { + "index": 10 + }, + { + "index": 11 + } + ] + }, + "ChargeParticle": { + "delay": 0.03, + "loop": true, + "frames": [ + { + "index": 12 + }, + { + "index": 13 + }, + { + "index": 1 + }, + { + "index": 3 + }, + { + "index": 14 + }, + { + "index": 13 + }, + { + "index": 12 + } + ] + }, + "Poof": { + "delay": 0.05, + "loop": true, + "frames": [ + { + "index": 23 + }, + { + "index": 24 + }, + { + "index": 25 + }, + { + "index": 26 + }, + { + "index": 27 + } + ] + } +} diff --git a/share/animation/Heroine.json b/share/animation/Heroine.json new file mode 100644 index 0000000..55ffb7d --- /dev/null +++ b/share/animation/Heroine.json @@ -0,0 +1,253 @@ +{ + "Stand": { + "type": "Stand", + "delay": 1, + "frames": [ + { + "index": 1, + "duration": 4 + }, + { + "index": 3, + "duration": 0.1 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 3, + "duration": 0.1 + }, + { + "index": 1, + "duration": 2 + }, + { + "index": 0, + "duration": 2 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 2, + "duration": 2 + }, + { + "index": 1, + "duration": 3 + }, + { + "index": 3, + "duration": 0.1 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 3, + "duration": 0.1 + }, + { + "index": 1, + "duration": 2 + }, + { + "index": 0, + "duration": 4 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 2, + "duration": 3 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 0, + "duration": 1 + }, + { + "index": 1, + "duration": 2 + }, + { + "index": 3, + "duration": 0.1 + }, + { + "index": 1, + "duration": 0.1 + }, + { + "index": 3, + "duration": 0.1 + } + ] + }, + "Blink": { + "type": "Stand", + "next": "Stand", + "delay": 0.25, + "frames": [ + { + "index": 3, + "duration": 0.25 + } + ] + }, + "Run": { + "type": "Run", + "delay": 0.25, + "frames": [ + { + "index": 4 + }, + { + "index": 5 + }, + { + "index": 6 + }, + { + "index": 7 + } + ] + }, + "StopRunning": { + "type": "Stand", + "next": "Stand", + "delay": 10, + "loop": true, + "frames": [ + { + "index": 8 + } + ] + }, + "Jump": { + "type": "Jump", + "next": "FlyDiagonallyUp", + "delay": 0.25, + "frames": [ + { + "index": 9, + "duration": 0.25 + } + ] + }, + "Land": { + "type": "Stand", + "next": "StopRunning", + "delay": 0.5, + "frames": [ + { + "index": 9, + "duration": 0.25 + } + ] + }, + "FlyDiagonallyUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 10 + }, + { + "index": 11 + } + ] + }, + "FlyDiagonallyDown": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 12 + }, + { + "index": 13 + } + ] + }, + "FlyStraightUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 0 + } + ] + }, + "FlyHorizontally": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 4 + }, + { + "index": 14 + } + ] + }, + "KnockedDown": { + "type": "Hit", + "delay": 0.1, + "frames": [ + { + "index": 17 + }, + { + "index": 18 + } + ] + }, + "Flattened": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 19 + } + ] + }, + "StartCharging": { + "type": "Charge", + "next": "Charge", + "delay": 0.2, + "frames": [ + { + "index": 21 + } + ] + }, + "Charge": { + "type": "Charge", + "delay": 0.2, + "loop": true, + "frames": [ + { + "index": 22 + } + ] + }, + "FireCharge": { + "type": "Charge", + "delay": 0.2, + "frames": [ + { + "index": 23 + } + ] + } +} diff --git a/share/animation/Jetbot.json b/share/animation/Jetbot.json new file mode 100644 index 0000000..240641a --- /dev/null +++ b/share/animation/Jetbot.json @@ -0,0 +1,179 @@ +{ + "Stand": { + "type": "Stand", + "delay": 1, + "frames": [ + { + "index": 0, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + }, + { + "index": 2, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + } + ] + }, + "Run": { + "type": "Run", + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + } + ] + }, + "StopRunning": { + "type": "Stand", + "next": "Stand", + "delay": 10, + "loop": true, + "frames": [ + { + "index": 6 + } + ] + }, + "Jump": { + "type": "Jump", + "next": "FlyDiagonallyUp", + "delay": 0.25, + "frames": [ + { + "index": 6 + } + ] + }, + "Land": { + "type": "Stand", + "next": "Stand", + "delay": 1, + "loop": true, + "frames": [ + { + "index": 6, + "duration": 0.25 + }, + { + "index": 0, + "duration": 0.25 + } + ] + }, + "FlyDiagonallyUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + } + ] + }, + "FlyDiagonallyDown": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + } + ] + }, + "FlyStraightUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + } + ] + }, + "FlyHorizontally": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 3 + }, + { + "index": 4 + } + ] + }, + "Punch": { + "type": "Attack", + "next": "Stand", + "delay": 0.05, + "frames": [ + { + "index": 3 + } + ] + }, + "StartFiring": { + "type": "Attack", + "next": "Fire", + "delay": 0.1, + "frames": [ + { + "index": 3 + } + ] + }, + "Fire": { + "type": "Attack", + "delay": 1, + "frames": [ + { + "index": 3, + "duration": 0.2 + } + ] + }, + "StopFiring": { + "type": "Attack", + "next": "Stand", + "delay": 0.1, + "frames": [ + { + "index": 3 + } + ] + }, + "KnockedDown": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 5 + } + ] + }, + "Flattened": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 6 + } + ] + } +} diff --git a/share/animation/RobotTrooper.json b/share/animation/RobotTrooper.json new file mode 100644 index 0000000..ed68a9b --- /dev/null +++ b/share/animation/RobotTrooper.json @@ -0,0 +1,203 @@ +{ + "Stand": { + "type": "Stand", + "delay": 1, + "frames": [ + { + "index": 0, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + }, + { + "index": 2, + "duration": 2 + }, + { + "index": 1, + "duration": 0.25 + } + ] + }, + "Run": { + "type": "Run", + "delay": 0.25, + "frames": [ + { + "index": 4 + }, + { + "index": 5 + }, + { + "index": 6 + }, + { + "index": 7 + } + ] + }, + "StopRunning": { + "type": "Stand", + "next": "Stand", + "delay": 10, + "loop": true, + "frames": [ + { + "index": 5 + } + ] + }, + "Jump": { + "type": "Jump", + "next": "FlyDiagonallyUp", + "delay": 0.25, + "frames": [ + { + "index": 17 + } + ] + }, + "Land": { + "type": "Stand", + "next": "Stand", + "delay": 1, + "loop": true, + "frames": [ + { + "index": 19, + "duration": 0.25 + }, + { + "index": 14, + "duration": 0.25 + } + ] + }, + "FlyDiagonallyUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 18 + } + ] + }, + "FlyDiagonallyDown": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 18 + } + ] + }, + "FlyStraightUp": { + "type": "Fly", + "delay": 0.1, + "frames": [ + { + "index": 18 + } + ] + }, + "FlyHorizontally": { + "type": "Fly", + "frames": [ + { + "index": 18 + } + ] + }, + "Punch": { + "type": "Attack", + "next": "Stand", + "delay": 0.05, + "frames": [ + { + "index": 8 + }, + { + "index": 9 + }, + { + "index": 10 + }, + { + "index": 11 + }, + { + "index": 12 + }, + { + "index": 13 + } + ] + }, + "StartFiring": { + "type": "Attack", + "next": "Fire", + "delay": 0.1, + "frames": [ + { + "index": 14 + }, + { + "index": 15 + }, + { + "index": 16 + } + ] + }, + "Fire": { + "type": "Attack", + "delay": 1, + "frames": [ + { + "index": 17, + "duration": 0.2 + }, + { + "index": 16, + "duration": 0.2 + } + ] + }, + "StopFiring": { + "type": "Attack", + "next": "Stand", + "delay": 0.1, + "frames": [ + { + "index": 16 + }, + { + "index": 15 + }, + { + "index": 14 + } + ] + }, + "KnockedDown": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 18 + } + ] + }, + "Flattened": { + "type": "Hit", + "delay": 1, + "frames": [ + { + "index": 19 + } + ] + } +} diff --git a/share/animations/AlienWarrior.xml b/share/animations/AlienWarrior.xml deleted file mode 100644 index e8b402b..0000000 --- a/share/animations/AlienWarrior.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/share/animations/BigExplosion.xml b/share/animations/BigExplosion.xml deleted file mode 100644 index c206ebc..0000000 --- a/share/animations/BigExplosion.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/Bonuses.xml b/share/animations/Bonuses.xml deleted file mode 100644 index 2d8c5da..0000000 --- a/share/animations/Bonuses.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/Bullets.xml b/share/animations/Bullets.xml deleted file mode 100644 index e7b9ca7..0000000 --- a/share/animations/Bullets.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/Effects.xml b/share/animations/Effects.xml deleted file mode 100644 index 5c278b2..0000000 --- a/share/animations/Effects.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/Heroine.xml b/share/animations/Heroine.xml deleted file mode 100644 index a07b909..0000000 --- a/share/animations/Heroine.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/Jetbot.xml b/share/animations/Jetbot.xml deleted file mode 100644 index d6d6944..0000000 --- a/share/animations/Jetbot.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/animations/RobotTrooper.xml b/share/animations/RobotTrooper.xml deleted file mode 100644 index 342592b..0000000 --- a/share/animations/RobotTrooper.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/share/scenes/Test.xml b/share/scene/Test.xml similarity index 100% rename from share/scenes/Test.xml rename to share/scene/Test.xml diff --git a/share/textures/AlienWarrior.data b/share/texture/AlienWarrior.data similarity index 100% rename from share/textures/AlienWarrior.data rename to share/texture/AlienWarrior.data diff --git a/share/textures/AlienWarrior.plist b/share/texture/AlienWarrior.plist similarity index 100% rename from share/textures/AlienWarrior.plist rename to share/texture/AlienWarrior.plist diff --git a/share/textures/AlienWarrior.png b/share/texture/AlienWarrior.png similarity index 100% rename from share/textures/AlienWarrior.png rename to share/texture/AlienWarrior.png diff --git a/share/textures/BackgroundFar.data b/share/texture/BackgroundFar.data similarity index 100% rename from share/textures/BackgroundFar.data rename to share/texture/BackgroundFar.data diff --git a/share/textures/BackgroundFar.plist b/share/texture/BackgroundFar.plist similarity index 100% rename from share/textures/BackgroundFar.plist rename to share/texture/BackgroundFar.plist diff --git a/share/textures/BackgroundFar.png b/share/texture/BackgroundFar.png similarity index 100% rename from share/textures/BackgroundFar.png rename to share/texture/BackgroundFar.png diff --git a/share/textures/BackgroundNear.data b/share/texture/BackgroundNear.data similarity index 100% rename from share/textures/BackgroundNear.data rename to share/texture/BackgroundNear.data diff --git a/share/textures/BackgroundNear.plist b/share/texture/BackgroundNear.plist similarity index 100% rename from share/textures/BackgroundNear.plist rename to share/texture/BackgroundNear.plist diff --git a/share/textures/BackgroundNear.png b/share/texture/BackgroundNear.png similarity index 100% rename from share/textures/BackgroundNear.png rename to share/texture/BackgroundNear.png diff --git a/share/textures/BigExplosion.data b/share/texture/BigExplosion.data similarity index 100% rename from share/textures/BigExplosion.data rename to share/texture/BigExplosion.data diff --git a/share/textures/BigExplosion.plist b/share/texture/BigExplosion.plist similarity index 100% rename from share/textures/BigExplosion.plist rename to share/texture/BigExplosion.plist diff --git a/share/textures/BigExplosion.png b/share/texture/BigExplosion.png similarity index 100% rename from share/textures/BigExplosion.png rename to share/texture/BigExplosion.png diff --git a/share/textures/Bonuses.data b/share/texture/Bonuses.data similarity index 100% rename from share/textures/Bonuses.data rename to share/texture/Bonuses.data diff --git a/share/textures/Bonuses.plist b/share/texture/Bonuses.plist similarity index 100% rename from share/textures/Bonuses.plist rename to share/texture/Bonuses.plist diff --git a/share/textures/Bonuses.png b/share/texture/Bonuses.png similarity index 100% rename from share/textures/Bonuses.png rename to share/texture/Bonuses.png diff --git a/share/textures/Building.data b/share/texture/Building.data similarity index 100% rename from share/textures/Building.data rename to share/texture/Building.data diff --git a/share/textures/Building.plist b/share/texture/Building.plist similarity index 100% rename from share/textures/Building.plist rename to share/texture/Building.plist diff --git a/share/textures/Building.png b/share/texture/Building.png similarity index 100% rename from share/textures/Building.png rename to share/texture/Building.png diff --git a/share/textures/Default.plist b/share/texture/Default.plist similarity index 100% rename from share/textures/Default.plist rename to share/texture/Default.plist diff --git a/share/textures/Font.data b/share/texture/Font.data similarity index 100% rename from share/textures/Font.data rename to share/texture/Font.data diff --git a/share/textures/Font.plist b/share/texture/Font.plist similarity index 100% rename from share/textures/Font.plist rename to share/texture/Font.plist diff --git a/share/textures/Font.png b/share/texture/Font.png similarity index 100% rename from share/textures/Font.png rename to share/texture/Font.png diff --git a/share/textures/Heroine.data b/share/texture/Heroine.data similarity index 100% rename from share/textures/Heroine.data rename to share/texture/Heroine.data diff --git a/share/textures/Heroine.plist b/share/texture/Heroine.plist similarity index 100% rename from share/textures/Heroine.plist rename to share/texture/Heroine.plist diff --git a/share/textures/Heroine.png b/share/texture/Heroine.png similarity index 100% rename from share/textures/Heroine.png rename to share/texture/Heroine.png diff --git a/share/textures/Jetbot.data b/share/texture/Jetbot.data similarity index 100% rename from share/textures/Jetbot.data rename to share/texture/Jetbot.data diff --git a/share/textures/Jetbot.plist b/share/texture/Jetbot.plist similarity index 100% rename from share/textures/Jetbot.plist rename to share/texture/Jetbot.plist diff --git a/share/textures/Jetbot.png b/share/texture/Jetbot.png similarity index 100% rename from share/textures/Jetbot.png rename to share/texture/Jetbot.png diff --git a/share/textures/Particles.data b/share/texture/Particles.data similarity index 100% rename from share/textures/Particles.data rename to share/texture/Particles.data diff --git a/share/textures/Particles.plist b/share/texture/Particles.plist similarity index 100% rename from share/textures/Particles.plist rename to share/texture/Particles.plist diff --git a/share/textures/Particles.png b/share/texture/Particles.png similarity index 100% rename from share/textures/Particles.png rename to share/texture/Particles.png diff --git a/share/textures/RobotTrooper.data b/share/texture/RobotTrooper.data similarity index 100% rename from share/textures/RobotTrooper.data rename to share/texture/RobotTrooper.data diff --git a/share/textures/RobotTrooper.plist b/share/texture/RobotTrooper.plist similarity index 100% rename from share/textures/RobotTrooper.plist rename to share/texture/RobotTrooper.plist diff --git a/share/textures/RobotTrooper.png b/share/texture/RobotTrooper.png similarity index 100% rename from share/textures/RobotTrooper.png rename to share/texture/RobotTrooper.png diff --git a/share/textures/Scenery.data b/share/texture/Scenery.data similarity index 100% rename from share/textures/Scenery.data rename to share/texture/Scenery.data diff --git a/share/textures/Scenery.plist b/share/texture/Scenery.plist similarity index 100% rename from share/textures/Scenery.plist rename to share/texture/Scenery.plist diff --git a/share/textures/Scenery.png b/share/texture/Scenery.png similarity index 100% rename from share/textures/Scenery.png rename to share/texture/Scenery.png diff --git a/share/textures/StatusBars.data b/share/texture/StatusBars.data similarity index 100% rename from share/textures/StatusBars.data rename to share/texture/StatusBars.data diff --git a/share/textures/StatusBars.plist b/share/texture/StatusBars.plist similarity index 100% rename from share/textures/StatusBars.plist rename to share/texture/StatusBars.plist diff --git a/share/textures/StatusBars.png b/share/texture/StatusBars.png similarity index 100% rename from share/textures/StatusBars.png rename to share/texture/StatusBars.png diff --git a/share/textures/TowerBlock1.data b/share/texture/TowerBlock1.data similarity index 100% rename from share/textures/TowerBlock1.data rename to share/texture/TowerBlock1.data diff --git a/share/textures/TowerBlock1.plist b/share/texture/TowerBlock1.plist similarity index 100% rename from share/textures/TowerBlock1.plist rename to share/texture/TowerBlock1.plist diff --git a/share/textures/TowerBlock1.png b/share/texture/TowerBlock1.png similarity index 100% rename from share/textures/TowerBlock1.png rename to share/texture/TowerBlock1.png diff --git a/share/textures/Trees.data b/share/texture/Trees.data similarity index 100% rename from share/textures/Trees.data rename to share/texture/Trees.data diff --git a/share/textures/Trees.plist b/share/texture/Trees.plist similarity index 100% rename from share/textures/Trees.plist rename to share/texture/Trees.plist diff --git a/share/textures/Trees.png b/share/texture/Trees.png similarity index 100% rename from share/textures/Trees.png rename to share/texture/Trees.png diff --git a/src/Makefile.am b/src/Makefile.am index a026f96..40dd173 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,8 @@ libdc_a_SOURCES = \ random.hh \ rectangle.cc \ rectangle.hh \ + resource.cc \ + resource.hh \ serializable.cc \ serializable.hh \ serializer.cc \ @@ -33,6 +35,7 @@ libdc_a_SOURCES = \ texture.cc \ texture.hh \ thread.hh \ + tilemap.hh \ timer.cc \ timer.hh \ vector.hh \ @@ -40,7 +43,7 @@ libdc_a_SOURCES = \ video.hh \ $(ENDLIST) -libdc_a_CPPFLAGS = -I/usr/include/SDL -I$(top_srcdir)/yajl/src -O3 +libdc_a_CPPFLAGS = -I/usr/include/SDL -I$(top_srcdir)/yajl/src -Wall #libdc_a_LDFLAGS = -lstdc++ -lSDL_image -lSDL_sound libdc_a_LIBADD = $(top_srcdir)/yajl/libyajl.a @@ -52,7 +55,7 @@ yoink_SOURCES = \ YoinkApp.hh \ $(ENDLIST) -yoink_CPPFLAGS = -I/usr/include/SDL -O3 +yoink_CPPFLAGS = -I/usr/include/SDL -Wall #yoink_LDFLAGS = -lstdc++ -lSDL_image -lSDL_sound -yoink_LDADD = libdc.a ../yajl/libyajl.a +yoink_LDADD = libdc.a libtinyxml.a ../yajl/libyajl.a diff --git a/src/animation.cc b/src/animation.cc new file mode 100644 index 0000000..745f0e4 --- /dev/null +++ b/src/animation.cc @@ -0,0 +1,50 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#include "animation.hh" + + +namespace dc { + + +class animation_impl +{ +public: + + class sequence + { + }; + + std::map sequences; +}; + + + + +} // namespace dc + diff --git a/src/animation.hh b/src/animation.hh new file mode 100644 index 0000000..85307ac --- /dev/null +++ b/src/animation.hh @@ -0,0 +1,64 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#ifndef _ANIMATION_HH_ +#define _ANIMATION_HH_ + +/** + * @file animation.hh + * Motion picture!! + */ + +#include + + +namespace dc { + + +class animation_impl; + +class animation : public resource +{ +public: + animation(const std::string& name); + + void setSequence(const std::string& sequence); + bool isSequenceDone(); + + void update(scalar dt); + unsigned getFrame(); + +private: + boost::shared_ptr impl; +}; + + +} // namespace dc + +#endif // _ANIMATION_HH_ + diff --git a/src/deserializer.cc b/src/deserializer.cc index abb6d01..4c3f8f2 100644 --- a/src/deserializer.cc +++ b/src/deserializer.cc @@ -72,7 +72,7 @@ public: void throwError() { unsigned char* errorMsg = yajl_get_error(hand, 0, 0, 0); - deserializer::parser_error error((char*)errorMsg); + deserializer::exception error((char*)errorMsg); yajl_free_error(hand, errorMsg); throw error; } diff --git a/src/deserializer.hh b/src/deserializer.hh index e424849..733ce39 100644 --- a/src/deserializer.hh +++ b/src/deserializer.hh @@ -61,9 +61,9 @@ public: serializable* pullNext(); void pop(); - struct parser_error : std::runtime_error + struct exception : std::runtime_error { - explicit parser_error(const std::string& what_arg) : + explicit exception(const std::string& what_arg) : std::runtime_error(what_arg) {} }; diff --git a/src/resource.cc b/src/resource.cc new file mode 100644 index 0000000..758827c --- /dev/null +++ b/src/resource.cc @@ -0,0 +1,88 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#include + +#include "resource.hh" + + +namespace dc { + + +std::vector resource::searchPaths_; + + +resource::resource(const std::string& name) throw(exception) +{ + filePath_ = getPathToResource(name); + + if (!filePath_.empty()) + { + throw exception("cannot find resource file " + name); + } +} + +resource::~resource() {} + + +const std::string& resource::getPathToFile() +{ + return filePath_; +} + +void resource::addSearchPath(const std::string& directory) +{ + if (directory[directory.length() - 1] != '/') + { + searchPaths_.push_back(directory + '/'); + } + else + { + searchPaths_.push_back(directory); + } +} + +std::string resource::getPathToResource(const std::string& name) +{ + std::vector::iterator i; + + for (i = searchPaths_.begin(); i != searchPaths_.end(); i++) + { + const char* fullPath = ((*i) + name).c_str(); + if (access(fullPath, R_OK) == 0) + { + return std::string(fullPath); + } + } + + return std::string(); +} + + +} // namespace dc + diff --git a/src/resource.hh b/src/resource.hh new file mode 100644 index 0000000..eff9e3e --- /dev/null +++ b/src/resource.hh @@ -0,0 +1,94 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#ifndef _RESOURCE_HH_ +#define _RESOURCE_HH_ + +/** + * @file resource.hh + * Parent class of textures, sounds, other assets. + */ + +#include +#include +#include + + +namespace dc { + + +/** + * Generic resource class. + */ + +class resource +{ +public: + struct exception : public std::runtime_error + { + explicit exception(const std::string& what_arg) : + std::runtime_error(what_arg) {} + }; + + resource(const std::string& name) throw(exception); + virtual ~resource(); + + /** + * Get the path of the file associated with this resource. + * @return Path. + */ + + const std::string& getPathToFile(); + + + /** + * Add a directory to search when looking for resource files. + * @param directory Path to a directory. + */ + + static void addSearchPath(const std::string& directory); + + /** + * Get the path to a resource of a given name. + * @param name Name of a resource. The name will be appended to each search + * directory in order. + * @return The first path found which resolves to a file. + */ + + static std::string getPathToResource(const std::string& name); + +private: + std::string filePath_; + static std::vector searchPaths_; +}; + + +} // namespace dc + +#endif // _RESOURCE_HH_ + diff --git a/src/serializer.cc b/src/serializer.cc index e2f7734..06563ef 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -65,13 +65,13 @@ public: switch (err) { case yajl_gen_generation_complete: - throw serializer::generator_error("the archive has already terminated"); + throw serializer::exception("the archive has already terminated"); case yajl_gen_keys_must_be_strings: - throw serializer::generator_error("map keys must be strings"); + throw serializer::exception("map keys must be strings"); case yajl_max_depth_exceeded: - throw serializer::generator_error("maximum archive depth exceeded"); + throw serializer::exception("maximum archive depth exceeded"); case yajl_gen_in_error_state: - throw serializer::generator_error("serializer already in error state"); + throw serializer::exception("serializer already in error state"); } } diff --git a/src/serializer.hh b/src/serializer.hh index 0690d6c..8e61a62 100644 --- a/src/serializer.hh +++ b/src/serializer.hh @@ -69,9 +69,9 @@ public: void flush(); - struct generator_error : std::runtime_error + struct exception : std::runtime_error { - explicit generator_error(const std::string& what_arg) : + explicit exception(const std::string& what_arg) : std::runtime_error(what_arg) {} }; diff --git a/src/settings.cc b/src/settings.cc index 475bdad..07ec0e5 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -88,7 +88,7 @@ void settings::loadFromFile(std::string filePath) " does not contain any valid settings." << std::endl; } } - catch (deserializer::parser_error e) + catch (deserializer::exception e) { std::cerr << "Cannot load settings from " << filePath << " because an exception was thrown: " << e.what() << std::endl; diff --git a/src/singleton.hh b/src/singleton.hh index 9cbb385..3b2ebf7 100644 --- a/src/singleton.hh +++ b/src/singleton.hh @@ -35,43 +35,43 @@ template class singleton { - static T* ptr; + static T* ptr_; public: singleton() { - if (!ptr) + if (!ptr_) { // This hack is from Game Programming Gems. long long offset = (long long)(T*)1 - (long long)(singleton*)(T*)1; - ptr = (T*)((long long)this + offset); + ptr_ = (T*)((long long)this + offset); } } ~singleton() { long long offset = (long long)(T*)1 - (long long)(singleton*)(T*)1; - if (ptr == (T*)((long long)this + offset)) + if (ptr_ == (T*)((long long)this + offset)) { - ptr = 0; + ptr_ = 0; } } static T& instance() { - if (!ptr) + if (!ptr_) { throw std::runtime_error("accessing uninstantiated singleton"); } - return *ptr; + return *ptr_; } - static T* instancePtr() + static T* instance__ptr() { - return ptr; + return ptr_; } }; -template T* singleton::ptr = 0; +template T* singleton::ptr_ = 0; #endif // _SINGLETON_HH_ diff --git a/src/texture.cc b/src/texture.cc index b91d23a..2f8f97a 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -26,7 +26,6 @@ *******************************************************************************/ -#include #include #include @@ -61,8 +60,8 @@ class texture_impl } public: - texture_impl(const std::string& filePath, bool keepInMemory) - : object(0), imageData(0), imagePath(filePath), keepData(keepInMemory) + texture_impl(texture* outside, bool keepInMemory) + : interface(outside), keepData(keepInMemory), object(0), imageData(0) { dispatcher::instance().addHandler("video.context_recreated", boost::bind(&texture_impl::contextRecreated, this, _1), @@ -92,20 +91,24 @@ public: return value; } - // Adapted from some public domain code. This code is common enough that it - // really should be included in SDL_image... static SDL_Surface* prepareImageForGL(SDL_Surface* surface) { - /* Use the surface width and height expanded to powers of 2 */ + // Adapted from some public domain code. This stuff is common enough + // that it really should be included in SDL_image... We need this + // because images loaded with SDL_image aren't exactly OpenGL-ready + // right out of the box. + int w = powerOfTwo(surface->w); int h = powerOfTwo(surface->h); + // 1. OpenGL images must (generally) have dimensions of a power-of-two. + SDL_Surface* image = SDL_CreateRGBSurface ( SDL_SWSURFACE, w, h, 32, -#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ +#if SDL_BYTEORDER == SDL_LIL_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, @@ -123,7 +126,6 @@ public: return 0; } - // Save the alpha blending attributes. Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); Uint8 savedAlpha = surface->format->alpha; if (savedFlags & SDL_SRCALPHA) @@ -132,44 +134,36 @@ public: } SDL_Rect srcArea, destArea; - /* Copy the surface into the GL texture image */ srcArea.x = 0; destArea.x = 0; - /* Copy it in at the bottom, because we're going to flip - this image upside-down in a moment - */ srcArea.y = 0; destArea.y = h - surface->h; srcArea.w = surface->w; srcArea.h = surface->h; SDL_BlitSurface(surface, &srcArea, image, &destArea); - /* Restore the alpha blending attributes */ if (savedFlags & SDL_SRCALPHA) { SDL_SetAlpha(surface, savedFlags, savedAlpha); } - /* Turn the image upside-down, because OpenGL textures - start at the bottom-left, instead of the top-left - */ + // 2. OpenGL textures make more sense when they are "upside down." + Uint8 line[image->pitch]; - /* These two make the following more readable */ Uint8 *pixels = static_cast(image->pixels); Uint16 pitch = image->pitch; int ybegin = 0; int yend = image->h - 1; - // TODO: consider if this lock is legal/appropriate - if (SDL_MUSTLOCK(image)) { SDL_LockSurface(image); } + if (SDL_MUSTLOCK(image)) SDL_LockSurface(image); while (ybegin < yend) { - memcpy(line, pixels + pitch*ybegin, pitch); - memcpy(pixels + pitch*ybegin, pixels + pitch*yend, pitch); - memcpy(pixels + pitch*yend, line, pitch); + memcpy(line, pixels + pitch * ybegin, pitch); + memcpy(pixels + pitch * ybegin, pixels + pitch * yend, pitch); + memcpy(pixels + pitch * yend, line, pitch); ybegin++; yend--; } - if (SDL_MUSTLOCK(image)) { SDL_UnlockSurface(image); } + if (SDL_MUSTLOCK(image)) SDL_UnlockSurface(image); return image; } @@ -179,11 +173,11 @@ public: { SDL_Surface* surface; - surface = IMG_Load(imagePath.c_str()); + surface = IMG_Load(interface->getPathToFile().c_str()); if (!surface) { - throw std::runtime_error("could not load image data from file"); + throw texture::exception("loading failed"); } imageData = prepareImageForGL(surface); @@ -191,7 +185,7 @@ public: if (!imageData) { - throw std::runtime_error("error in preparing image data for GL"); + throw texture::exception(""); } if (imageData->format->BytesPerPixel == 3) @@ -205,7 +199,7 @@ public: else { SDL_FreeSurface(imageData); - throw std::runtime_error("image must be 24 or 32 bpp"); + throw texture::exception("image is not the required 24 or 32 bpp"); } width = imageData->w; @@ -243,9 +237,8 @@ public: imageData->pixels ); - // These default filters can be changed later... glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if (!keepData) { @@ -260,28 +253,23 @@ public: int mode; GLuint object; - std::string imagePath; bool keepData; SDL_Surface* imageData; + + texture* interface; }; -texture::texture(const std::string& filePath, bool keepInMemory) - : impl(new texture_impl(filePath, keepInMemory)) {} - - -const std::string& texture::filePath() -{ - return impl->imagePath; -} +texture::texture(const std::string& name, bool keepInMemory) + : resource(name), impl(new texture_impl(this, keepInMemory)) {} void texture::bind() { - glBindTexture(GL_TEXTURE_2D, object()); + glBindTexture(GL_TEXTURE_2D, getObject()); } -GLuint texture::object() +GLuint texture::getObject() { if (!impl->object) { @@ -292,17 +280,26 @@ GLuint texture::object() } -unsigned texture::width() +unsigned texture::getWidth() { + if (!impl->object) + { + impl->uploadToGL(); + } + return impl->width; } -unsigned texture::height() +unsigned texture::getHeight() { + if (!impl->object) + { + impl->uploadToGL(); + } + return impl->height; } - } // namespace dc diff --git a/src/texture.hh b/src/texture.hh index 6fa4e32..1be4c5a 100644 --- a/src/texture.hh +++ b/src/texture.hh @@ -29,8 +29,16 @@ #ifndef _TEXTURE_HH_ #define _TEXTURE_HH_ +/** + * @file texture.hh + * Image-loading and OpenGL texture loading. + */ + +#include + #include +#include "resource.hh" #include "opengl.hh" @@ -39,18 +47,22 @@ namespace dc { class texture_impl; -class texture +class texture : public resource { public: - texture(const std::string& filePath, bool keepInMemory = false); - - const std::string& filePath(); + texture(const std::string& name, bool keepInMemory = false); void bind(); - GLuint object(); + GLuint getObject(); + + unsigned getWidth(); + unsigned getHeight(); - unsigned width(); - unsigned height(); + struct exception : std::runtime_error + { + explicit exception(const std::string& what_arg) : + std::runtime_error(what_arg) {} + }; private: boost::shared_ptr impl; diff --git a/src/tilemap.hh b/src/tilemap.hh new file mode 100644 index 0000000..2121953 --- /dev/null +++ b/src/tilemap.hh @@ -0,0 +1,157 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#ifndef _TILEMAP_HH_ +#define _TILEMAP_HH_ + +/** + * @file tilemap.hh + * Small subclass to give some basic tile-mapping functionality to textures. + */ + +#include + +#include + +#include "opengl.hh" +#include "texture.hh" + + +namespace dc { + + +namespace tile { + +/** + * Possible orientations for texture coordinates. + */ + +typedef enum +{ + normal = 0, ///< Normal orientation. + flip = 1, ///< Flip over a horizontal axis. + reverse = 2, ///< Flip over a vertical axis. + flip_and_reverse = 3 ///< Flip over both. +} orientation; + +} // namespace tile + + +/** + * A tilemap is a texture which is meant to be divided into smaller sprites. + * This class provides all the functionality of a texture and adds some methods + * to get texture coordinates to individual tiles within the tilemap. For + * simplicity, this class assumes square tiles. + */ + +class tilemap : public texture +{ +public: + tilemap(const std::string& filePath, bool keepInMemory = false, + unsigned tilesU = 1, unsigned tilesV = 1) + : texture(filePath, keepInMemory), tilesU_(tilesU), tilesV_(tilesV) {} + + + /** + * Set the number of rows and columns of square tiles. + * @param tilesU Columns of tiles. + * @param tilesV Rows of tiles. + */ + + void setTileDimensions(unsigned tilesU, unsigned tilesV) + { + tilesU_ = tilesU; + tilesV_ = tilesV; + } + + + /** + * Calculate texture coordinates for a tile at a certain index. Tiles are + * indexed start with zero as the to-left tile and moving across, then down. + * @param index The tile index. + * @param coords An array of floats where the texture coordinates will be + * stored after this call. The first coordinate (u,v) will be in the first + * two places and so on until all four coordinates are stored, therefore + * requiring enough room for an array of eight floats. The winding of the + * coordinates is always counter-clockwise (the GL default). + */ + + void getTileCoords(unsigned index, float coords[8]) + { + assert(index < tilesU_ * tilesV_); + + float w = 1.0 / float(tilesU_); + float h = 1.0 / float(tilesV_); + + coords[0] = float(index % tilesU_) * w; + coords[1] = (float(tilesV_ - 1) - float(index / tilesU_)) * h; + coords[2] = coords[0] + w; + coords[3] = coords[1]; + coords[4] = coords[2]; + coords[5] = coords[1] + h; + coords[6] = coords[0]; + coords[7] = coords[5]; + } + + /** + * This version let's you specify an orientation that will be reflected in + * the texture coordinates. This allows you to easily map a texture + * backwards or upside-down. + * @param what The orientation; can be flip, reverse, or flip_and_reverse. + */ + + void getTileCoords(unsigned index, float coords[8], tile::orientation what) + { + getTileCoords(index, coords); + + if (what & tile::flip) + { + coords[1] = coords[5]; + coords[5] = coords[3]; + coords[3] = coords[7]; + coords[7] = coords[5]; + } + if (what & tile::reverse) + { + coords[0] = coords[2]; + coords[2] = coords[6]; + coords[4] = coords[6]; + coords[6] = coords[0]; + } + } + +private: + unsigned tilesU_; + unsigned tilesV_; +}; + + +} // namespace dc + +#endif // _TILEMAP_HH_ + -- 2.45.2