├── die.wav ├── no.wav ├── scores ├── arrow.png ├── ball.png ├── bing.wav ├── font.otf ├── menu.png ├── mute.png ├── sound.png ├── thud.wav ├── hurray.wav ├── playing.png ├── highscore.png ├── recording.png ├── menu-restart.png ├── menu-resume.png ├── menu-settings.png ├── kattMickisShooter.icns ├── kattMickisShooter.mp3 ├── kattMickisShooter.ogg ├── screencaps ├── bubbleshooter.jpg ├── bubbleshooter_newgame.jpg └── bubbleshooter_highscore.jpg ├── .gitignore ├── SDLMain.h ├── README.md ├── Makefile ├── lib.h ├── sound.h ├── settingscreen.cpp ├── newgamenode.h ├── settingsnode.h ├── highscorenode.h ├── cancelnode.h ├── continuenode.h ├── submitnode.h ├── debug.h ├── settingscreen.h ├── homescreen.h ├── unicode.h ├── cancelnode.cpp ├── background.cpp ├── submitnode.cpp ├── continuenode.cpp ├── lib.cpp ├── background.h ├── music.h ├── settingsnode.cpp ├── newgamenode.cpp ├── points.h ├── inputnode.h ├── node.cpp ├── main.cpp ├── mysdl.h ├── sprite.cpp ├── highscorenode.cpp ├── ballmanager.h ├── highscorescreen.h ├── homescreen.cpp ├── highscoremanager.h ├── fx.h ├── menu.h ├── scorescrollnode.h ├── node.h ├── screen.h ├── abstract_inputnode.h ├── unicode.cpp ├── fx.cpp ├── arrow.h ├── sound.cpp ├── inputnode.cpp ├── points.cpp ├── vector.h ├── music.cpp ├── sprite.h ├── grid.h ├── debug.cpp ├── highscoremanager.cpp ├── menu.cpp ├── highscorescreen.cpp ├── ball.h ├── mysdl.cpp ├── screen.cpp ├── vector.cpp ├── ballmanager.cpp ├── abstract_inputnode.cpp ├── game.h ├── scorescrollnode.cpp ├── arrow.cpp ├── ball.cpp ├── grid.cpp ├── game.cpp └── SDLMain.m /die.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/die.wav -------------------------------------------------------------------------------- /no.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/no.wav -------------------------------------------------------------------------------- /scores: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/scores -------------------------------------------------------------------------------- /arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/arrow.png -------------------------------------------------------------------------------- /ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/ball.png -------------------------------------------------------------------------------- /bing.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/bing.wav -------------------------------------------------------------------------------- /font.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/font.otf -------------------------------------------------------------------------------- /menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/menu.png -------------------------------------------------------------------------------- /mute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/mute.png -------------------------------------------------------------------------------- /sound.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/sound.png -------------------------------------------------------------------------------- /thud.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/thud.wav -------------------------------------------------------------------------------- /hurray.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/hurray.wav -------------------------------------------------------------------------------- /playing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/playing.png -------------------------------------------------------------------------------- /highscore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/highscore.png -------------------------------------------------------------------------------- /recording.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/recording.png -------------------------------------------------------------------------------- /menu-restart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/menu-restart.png -------------------------------------------------------------------------------- /menu-resume.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/menu-resume.png -------------------------------------------------------------------------------- /menu-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/menu-settings.png -------------------------------------------------------------------------------- /kattMickisShooter.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/kattMickisShooter.icns -------------------------------------------------------------------------------- /kattMickisShooter.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/kattMickisShooter.mp3 -------------------------------------------------------------------------------- /kattMickisShooter.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/kattMickisShooter.ogg -------------------------------------------------------------------------------- /screencaps/bubbleshooter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/screencaps/bubbleshooter.jpg -------------------------------------------------------------------------------- /screencaps/bubbleshooter_newgame.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/screencaps/bubbleshooter_newgame.jpg -------------------------------------------------------------------------------- /screencaps/bubbleshooter_highscore.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erkie/KM-Bubble-Shooter/HEAD/screencaps/bubbleshooter_highscore.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | kattMickisShooter.xcodeproj 2 | kattMickisShooter_Prefix.pch 3 | Info.plist 4 | English.lproj 5 | build 6 | SDL_gfx 7 | Sounds/*_data 8 | Sounds/*.aup* 9 | *.psd 10 | *.o 11 | Thumbs.db 12 | Makefile.win 13 | icon.ico 14 | kattMickisShooter.dev 15 | kattMickisShooter.ico 16 | kattMickisShooter_* 17 | km-bubble-shooter 18 | -------------------------------------------------------------------------------- /SDLMain.h: -------------------------------------------------------------------------------- 1 | /* SDLMain.m - main entry point for our Cocoa-ized SDL app 2 | Initial Version: Darrell Walisser 3 | Non-NIB-Code & other changes: Max Horn 4 | 5 | Feel free to customize this file to suit your needs 6 | */ 7 | 8 | #ifndef _SDLMain_h_ 9 | #define _SDLMain_h_ 10 | 11 | #import 12 | 13 | @interface SDLMain : NSObject 14 | @end 15 | 16 | #endif /* _SDLMain_h_ */ 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hello. Discussion on my code is welcome. 2 | 3 | This game has been successfully compiled on Mac OS X. 4 | Windows and Fedora Linux testers/scripts wanted... 5 | 6 | 7 | Screen shots: 8 | ============= 9 | ![alt text](https://raw.github.com/davehorner/KM-Bubble-Shooter/master/screencaps/bubbleshooter.jpg "main screen") 10 | ![alt text](https://raw.github.com/davehorner/KM-Bubble-Shooter/master/screencaps/bubbleshooter_highscore.jpg "high score") 11 | ![alt text](https://raw.github.com/davehorner/KM-Bubble-Shooter/master/screencaps/bubbleshooter_newgame.jpg "newgame") 12 | 13 | Installation: 14 | ============= 15 | 16 | ### Mac ### 17 | 18 | $ brew install libvorbis sdl sdl_ttf sdl_mixer sdl_gfx sdl_image 19 | $ make 20 | $ ./km-bubble-shooter 21 | $ # party 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS= -I/usr/local/include/ -I/usr/local/include/SDL 3 | LDFLAGS=-lSDL_ttf -lSDL_mixer -lSDL_gfx -lSDL_image -I/usr/local/include/ -I/usr/local/include/SDL 4 | SDLFLAGS=`sdl-config --cflags --libs` 5 | SOURCES=SDLMain.m unicode.cpp mysdl.cpp node.cpp screen.cpp abstract_inputnode.cpp arrow.cpp background.cpp ball.cpp ballmanager.cpp cancelnode.cpp continuenode.cpp debug.cpp fx.cpp game.cpp grid.cpp highscoremanager.cpp highscorenode.cpp highscorescreen.cpp homescreen.cpp inputnode.cpp lib.cpp main.cpp menu.cpp music.cpp newgamenode.cpp points.cpp scorescrollnode.cpp settingscreen.cpp settingsnode.cpp sound.cpp sprite.cpp submitnode.cpp vector.cpp 6 | OBJECTS=$(SOURCES:.cpp=.o) 7 | EXECUTABLE=km-bubble-shooter 8 | 9 | all: $(SOURCES) $(EXECUTABLE) 10 | 11 | clean: 12 | rm ./km-bubble-shooter 13 | 14 | $(EXECUTABLE): 15 | $(CC) $(LDFLAGS) $(SDLFLAGS) $(SOURCES) -o $@ 16 | 17 | .cpp.o: 18 | $(CC) $(CFLAGS) $(SDLFLAGS) $< -o $@ 19 | 20 | .m.o: 21 | $(CC) $(CFLAGS) $(SDLFLAGS) $< -o $@ 22 | -------------------------------------------------------------------------------- /lib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * lib.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-08-17. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef __LIB_H__ 28 | #define __LIB_H__ 29 | 30 | #include "SDL.h" 31 | 32 | namespace Lib { 33 | SDL_Surface *loadImage(const char *filename); 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /sound.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sound.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __SOUND_H__ 29 | #define __SOUND_H__ 30 | 31 | void play_music(); 32 | void play_ball_bounce(); 33 | void play_ball_thud(); 34 | void play_ball_die(); 35 | void play_lose(); 36 | void play_win(); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /settingscreen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * settingscreen.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "settingscreen.h" 29 | 30 | SettingsScreen::SettingsScreen(Menu *menu): Screen(menu) 31 | { 32 | 33 | } 34 | 35 | void SettingsScreen::draw(SDL_Surface *screen) 36 | { 37 | drawNodes(screen); 38 | } -------------------------------------------------------------------------------- /newgamenode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * newgamenode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __NEWGAMENODE_H__ 29 | #define __NEWGAMENODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class NewGameNode: public Node 34 | { 35 | public: 36 | NewGameNode(Screen *screen); 37 | 38 | void onclick(); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /settingsnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * settingsnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __SETTINGSNODE_H__ 29 | #define __SETTINGSNODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class SettingsNode: public Node 34 | { 35 | public: 36 | SettingsNode(Screen *screen); 37 | void onclick(); 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /highscorenode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * highscorenode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __HIGHSCORENODE_H__ 29 | #define __HIGHSCORENODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class Screen; 34 | 35 | class HighscoreNode: public Node 36 | { 37 | public: 38 | HighscoreNode(Screen *); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /cancelnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cancelnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __CANCELNODE_H__ 29 | #define __CANCELNODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class Screen; 34 | 35 | class CancelNode: public Node 36 | { 37 | public: 38 | CancelNode(Screen *); 39 | void onclick(); 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /continuenode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * continuenode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __CONTINUENODE_H__ 29 | #define __CONTINUENODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class ContinueNode: public Node 34 | { 35 | public: 36 | ContinueNode(Screen *screen); 37 | 38 | void onclick(); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /submitnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * submitnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | 29 | #ifndef __SUBMITNODE_H__ 30 | #define __SUBMITNODE_H__ 31 | 32 | #include "node.h" 33 | 34 | class Screen; 35 | 36 | class SubmitNode: public Node 37 | { 38 | public: 39 | SubmitNode(Screen *); 40 | void onclick(); 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-18. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __DEBUG_H__ 29 | #define __DEBUG_H__ 30 | 31 | #include 32 | #include "SDL.h" 33 | 34 | void print_pixel_at(const char *name, SDL_Surface *surface, int x, int y); 35 | 36 | std::ostream & operator<<(std::ostream &os, const SDL_Rect &r ); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /settingscreen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * settingscreen.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __SETTINGSSCREEN_H__ 29 | #define __SETTINGSSCREEN_H__ 30 | 31 | #include "menu.h" 32 | #include "screen.h" 33 | 34 | class SettingsScreen: public Screen 35 | { 36 | public: 37 | SettingsScreen(Menu *menu); 38 | void draw(SDL_Surface *); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /homescreen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * homescreen.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __HOMESCREEN_H__ 29 | #define __HOMESCREEN_H__ 30 | 31 | #include "SDL.h" 32 | 33 | #include "menu.h" 34 | #include "screen.h" 35 | 36 | class HomeScreen: public Screen 37 | { 38 | public: 39 | HomeScreen(Menu *menu); 40 | void draw(SDL_Surface *); 41 | }; 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /unicode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * unicode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-02-16. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | * A collection of simple unicode text operations 27 | */ 28 | 29 | #ifndef __UNICODE_H__ 30 | #define __UNICODE_H__ 31 | 32 | #include "SDL.h" 33 | 34 | int uni_length(const Uint16 *text); 35 | void uni_place(Uint16 *text, Uint16 ch, int arr_size, int index); 36 | void uni_slice(Uint16 *text, int arr_size, int index); 37 | 38 | #endif -------------------------------------------------------------------------------- /cancelnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * cancelnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL.h" 29 | 30 | #include "menu.h" 31 | #include "screen.h" 32 | #include "cancelnode.h" 33 | 34 | CancelNode::CancelNode(Screen *screen): Node(screen) 35 | { 36 | _pos.x = 106; 37 | _pos.y = 115; 38 | _pos.w = 28; 39 | _pos.h = 29; 40 | } 41 | 42 | void CancelNode::onclick() 43 | { 44 | _screen->menu()->showScreen(Menu::Home); 45 | } 46 | -------------------------------------------------------------------------------- /background.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * background.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-20. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "background.h" 31 | #include "game.h" 32 | 33 | Background::Background(Game *game): Sprite(game) 34 | { 35 | _color = SDL_MapRGBA(_game->buffer()->format, 92, 110, 114, SDL_ALPHA_OPAQUE); 36 | } 37 | 38 | void Background::draw() 39 | { 40 | if ( _game->isPaused() ) 41 | return; 42 | 43 | SDL_FillRect(_game->buffer(), NULL, _color); 44 | } 45 | -------------------------------------------------------------------------------- /submitnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * submitnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL.h" 29 | 30 | #include "highscorescreen.h" 31 | 32 | #include "menu.h" 33 | #include "screen.h" 34 | #include "submitnode.h" 35 | 36 | SubmitNode::SubmitNode(Screen *screen): Node(screen) 37 | { 38 | _pos.x = 1; 39 | _pos.y = 113; 40 | _pos.w = 96; 41 | _pos.h = 33; 42 | } 43 | 44 | void SubmitNode::onclick() 45 | { 46 | ((HighscoreScreen *)_screen)->submit(); 47 | } 48 | -------------------------------------------------------------------------------- /continuenode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * continuenode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL_image.h" 29 | 30 | #include "game.h" 31 | #include "screen.h" 32 | #include "continuenode.h" 33 | 34 | ContinueNode::ContinueNode(Screen *screen): Node(screen) 35 | { 36 | _image = IMG_Load("menu-resume.png"); 37 | 38 | _pos = _image->clip_rect; 39 | _pos.x = 0; 40 | _pos.y = 10; 41 | } 42 | 43 | void ContinueNode::onclick() 44 | { 45 | _screen->game()->start(); 46 | } 47 | -------------------------------------------------------------------------------- /lib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * lib.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-08-17. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "lib.h" 28 | 29 | #include 30 | 31 | #include "SDL.h" 32 | #include "SDL_image.h" 33 | 34 | namespace Lib { 35 | SDL_Surface *loadImage(const char *filename) 36 | { 37 | SDL_Surface *img = IMG_Load(filename); 38 | if ( ! img ) 39 | { 40 | std::cerr << "Could not load image \"" << filename << "\": " << IMG_GetError() << std::endl; 41 | return false; 42 | } 43 | return img; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /background.h: -------------------------------------------------------------------------------- 1 | /* 2 | * background.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-20. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __BACKGROUND_H__ 29 | #define __BACKGROUND_H__ 30 | 31 | #include "SDL.h" 32 | #include "sprite.h" 33 | 34 | class Background: public Sprite 35 | { 36 | private: 37 | SDL_Rect _pos; 38 | Uint32 _color; 39 | public: 40 | Background(Game *game); 41 | void draw(); 42 | void tick() {}; 43 | void handleEvent(const SDL_Event &event) {}; 44 | 45 | Uint32 color() { return _color; }; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /music.h: -------------------------------------------------------------------------------- 1 | /* 2 | * music.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-08-17. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef __MUSIC_H__ 28 | #define __MUSIC_H__ 29 | 30 | #include "SDL.h" 31 | 32 | #include "game.h" 33 | #include "sprite.h" 34 | 35 | class Music: public Sprite 36 | { 37 | SDL_Surface *_is_playing; 38 | SDL_Surface *_is_mute; 39 | 40 | SDL_Rect _default_pos; 41 | SDL_Rect _paused_pos; 42 | public: 43 | Music(Game *); 44 | void tick(); 45 | void draw(); 46 | void handleEvent(const SDL_Event &event); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /settingsnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * settingsnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL_image.h" 29 | 30 | #include "menu.h" 31 | #include "screen.h" 32 | #include "settingsnode.h" 33 | 34 | SettingsNode::SettingsNode(Screen *screen): Node(screen) 35 | { 36 | _image = IMG_Load("menu-settings.png"); 37 | 38 | _pos = _image->clip_rect; 39 | _pos.x = 0; 40 | _pos.y = 90; 41 | } 42 | 43 | void SettingsNode::onclick() 44 | { 45 | _screen->menu()->showScreen(Menu::Settings); 46 | } 47 | -------------------------------------------------------------------------------- /newgamenode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * newgamenode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL_image.h" 29 | 30 | #include "game.h" 31 | #include "screen.h" 32 | #include "newgamenode.h" 33 | 34 | NewGameNode::NewGameNode(Screen *screen): Node(screen) 35 | { 36 | _image = IMG_Load("menu-restart.png"); 37 | 38 | _pos = _image->clip_rect; 39 | _pos.x = 0; 40 | _pos.y = 50; 41 | } 42 | 43 | void NewGameNode::onclick() 44 | { 45 | _screen->game()->reset(); 46 | _screen->game()->start(); 47 | } 48 | -------------------------------------------------------------------------------- /points.h: -------------------------------------------------------------------------------- 1 | /* 2 | * points.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-20. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __POINTS_H__ 29 | #define __POINTS_H__ 30 | 31 | #include "SDL.h" 32 | #include "SDL/SDL_ttf.h" 33 | 34 | #include "sprite.h" 35 | 36 | class Points: public Sprite 37 | { 38 | private: 39 | TTF_Font *_font; 40 | SDL_Color _font_color; 41 | 42 | long _last_points; 43 | public: 44 | Points(Game *game); 45 | ~Points(); 46 | void tick(); 47 | void draw(); 48 | 49 | void renderPoints(long points); 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /inputnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * inputnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | 29 | #ifndef __INPUTNODE_H__ 30 | #define __INPUTNODE_H__ 31 | 32 | #include "SDL.h" 33 | #include "SDL/SDL_ttf.h" 34 | 35 | #include "abstract_inputnode.h" 36 | 37 | class Screen; 38 | 39 | class InputNode: public Abstract::InputNode 40 | { 41 | public: 42 | InputNode(Screen *screen); 43 | 44 | void onclick(); 45 | void ontype(Uint16, SDLKey); 46 | void onfocus(); 47 | void onblur(); 48 | void submit(); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * node.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "screen.h" 31 | #include "node.h" 32 | 33 | Node::Node(Screen *screen): _screen(screen), _image(NULL) 34 | { 35 | _scroll.x = _scroll.y = 0; 36 | _scroll.w = _scroll.h = -1; // I think this means the entire surface. It works atleast... 37 | } 38 | 39 | void Node::draw(SDL_Surface *surface) 40 | { 41 | SDL_Rect rect = _screen->makeRect(_pos); 42 | SDL_BlitSurface(_image, &_scroll, surface, &rect); 43 | } 44 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Bubble Shooter clone for kattMickis 4 | 5 | By: Erik Andersson 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "SDL.h" 13 | #include "SDL_image.h" 14 | #include "SDL/SDL_ttf.h" 15 | #include "SDL_mixer.h" 16 | 17 | #include "unicode.h" 18 | 19 | #include "game.h" 20 | 21 | const int SCREEN_WIDTH = 403; 22 | const int SCREEN_HEIGHT = 405; 23 | const int SCREEN_BPP = 32; 24 | 25 | extern "C" int main(int argc, char *argv[]) 26 | { 27 | // Seed random number generator 28 | srand(time(NULL)); 29 | 30 | // Init SDL 31 | Uint32 init_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO; 32 | Uint32 screen_flags = SDL_HWSURFACE; 33 | 34 | if ( SDL_Init(init_flags) < 0 ) 35 | { 36 | std::cout << "Error initializing SDL: " << SDL_GetError(); 37 | return 1; 38 | } 39 | 40 | SDL_EnableUNICODE(1); 41 | 42 | if ( TTF_Init() < 0 ) 43 | { 44 | std::cout << "Error initializing TTF: " << TTF_GetError(); 45 | return 2; 46 | } 47 | 48 | int audio_rate = 22050; 49 | Uint16 audio_format = AUDIO_S16SYS; 50 | int audio_channels = 2; 51 | int audio_buffers = 1024; 52 | 53 | if ( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0 ) 54 | { 55 | std::cout << "Error initialize audio: " << Mix_GetError(); 56 | return 3; 57 | } 58 | 59 | // Enable key repeat 60 | SDL_EnableKeyRepeat(250, 100); 61 | 62 | SDL_Surface *screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, screen_flags); 63 | if ( screen == NULL ) 64 | { 65 | std::cout << "Error creating screen: " << SDL_GetError(); 66 | return 4; 67 | } 68 | 69 | SDL_WM_SetCaption("kattMickis Bubble Shooter", NULL); 70 | 71 | Game game = Game(screen); 72 | game.setWidth(SCREEN_WIDTH); 73 | game.setHeight(SCREEN_HEIGHT); 74 | game.loop(); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /mysdl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * mysdl.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-19. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | * things_that_sdl_should_do_by_default 27 | */ 28 | 29 | #ifndef __MYSDL_H__ 30 | #define __MYSDL_H__ 31 | 32 | #include "SDL.h" 33 | #include "SDL/SDL_ttf.h" 34 | 35 | void draw_transparent_surface_onto_empty_surface(SDL_Rect src_rect, SDL_Surface *empty, SDL_Surface *src); 36 | SDL_Surface *change_opacity_of_surface(SDL_Surface *src, Uint8 opacity); 37 | 38 | bool check_rect_collision(SDL_Rect *rect, int x, int y); 39 | 40 | TTF_Font *load_font(int size); 41 | 42 | SDL_Rect get_rect_intersection(SDL_Rect one, SDL_Rect two); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /sprite.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * sprite.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-30. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "mysdl.h" 29 | #include "game.h" 30 | #include "sprite.h" 31 | 32 | int Sprite::CID = 0; 33 | 34 | void Sprite::setImage(SDL_Surface *s) 35 | { 36 | _image = s; 37 | _is_dirty = true; 38 | } 39 | 40 | void Sprite::preTick() 41 | { 42 | _is_dirty = false; 43 | _old_rect = _rect; 44 | _old_image = _image; 45 | } 46 | 47 | void Sprite::postTick() 48 | { 49 | if ( _old_rect.x != _rect.x || _old_rect.y != _rect.y || _old_rect.w != _rect.w || _old_rect.h != _rect.h || _old_image != _image ) 50 | { 51 | _is_dirty = true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /highscorenode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * highscorenode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL.h" 29 | #include "SDL/SDL_ttf.h" 30 | 31 | #include "mysdl.h" 32 | 33 | #include "screen.h" 34 | #include "node.h" 35 | #include "scorescrollnode.h" 36 | #include "highscorenode.h" 37 | 38 | HighscoreNode::HighscoreNode(Screen *screen): Node(screen) 39 | { 40 | TTF_Font *font = load_font(16); 41 | SDL_Color color; 42 | color.r = color.g = color.b = 0xFF; 43 | _image = TTF_RenderText_Blended(font, "Highscores", color); 44 | 45 | _pos = _image->clip_rect; 46 | _pos.x = 175; 47 | _pos.y = 10; 48 | 49 | _screen->addNode(new ScoreScrollNode(_screen)); 50 | } 51 | -------------------------------------------------------------------------------- /ballmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ballmanager.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-19. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. * 25 | */ 26 | 27 | #ifndef __BALLMANAGER_H__ 28 | #define __BALLMANAGER_H__ 29 | 30 | #include 31 | 32 | #include "ball.h" 33 | #include "grid.h" 34 | 35 | const int BALLMANAGER_COLORS = 6; 36 | 37 | class BallManager 38 | { 39 | private: 40 | static SDL_Surface* _image; 41 | static std::vector _available; 42 | static SDL_Surface* _balls[BALLMANAGER_COLORS]; 43 | public: 44 | static SDL_Surface *load(Ball::Colors); 45 | static Ball::Colors randomColor(); 46 | static void prepRemList(Grid *grid); 47 | static Ball::Colors randomRemainingColor(Grid *grid); 48 | static bool colorExists(Ball::Colors check); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /highscorescreen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * highscorescreen.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __HIGHSCORESCREEN_H__ 29 | #define __HIGHSCORESCREEN_H__ 30 | 31 | #include "SDL.h" 32 | #include "SDL/SDL_ttf.h" 33 | 34 | #include "menu.h" 35 | #include "screen.h" 36 | 37 | class HighscoreScreen: public Screen 38 | { 39 | private: 40 | SDL_Surface *_image, *_text, *_points; 41 | SDL_Rect _controls, _text_rect, _points_rect; 42 | 43 | SDL_Color _fg; 44 | TTF_Font *_font; 45 | 46 | Uint16 _name[40]; 47 | public: 48 | HighscoreScreen(Menu *menu); 49 | 50 | void name(Uint16 name[40]) 51 | { 52 | for ( int i = 0; i < 40; i++ ) 53 | _name[i] = name[i]; 54 | }; 55 | 56 | void draw(SDL_Surface *surface); 57 | void renderPointsText(); 58 | 59 | void submit(); 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /homescreen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * homescreen.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL.h" 29 | #include "SDL_image.h" 30 | 31 | #include "highscoremanager.h" 32 | 33 | // Nodes 34 | #include "continuenode.h" 35 | #include "newgamenode.h" 36 | #include "settingsnode.h" 37 | #include "highscorenode.h" 38 | 39 | #include "homescreen.h" 40 | 41 | HomeScreen::HomeScreen(Menu *menu): Screen(menu) 42 | { 43 | _pos.w = 308; 44 | _pos.h = 170; 45 | _pos.x = _menu->centerX() - _pos.w / 2; 46 | _pos.y = _menu->centerY() - _pos.h / 2 + 20; 47 | 48 | addNode(new ContinueNode(this)); 49 | addNode(new NewGameNode(this)); 50 | //addNode(new SettingsNode(this)); 51 | addNode(new HighscoreNode(this)); 52 | } 53 | 54 | void HomeScreen::draw(SDL_Surface *surface) 55 | { 56 | drawNodes(surface); 57 | } 58 | -------------------------------------------------------------------------------- /highscoremanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * highscoremanager.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __HIGHSCOREMANAGER_H__ 29 | #define __HIGHSCOREMANAGER_H__ 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include "SDL.h" 36 | 37 | const long HIGHSCORES_SAVED = 50; 38 | 39 | struct Highscore 40 | { 41 | long score; 42 | Uint16 name[40]; 43 | }; 44 | 45 | typedef std::list highscore_list; 46 | 47 | bool inline comp_highscore(Highscore *one, Highscore *two) 48 | { 49 | return one->score > two->score; 50 | } 51 | 52 | class HighscoreManager 53 | { 54 | private: 55 | highscore_list _scores; 56 | public: 57 | HighscoreManager(); 58 | void load(); 59 | highscore_list &get() { return _scores; } 60 | void save(); 61 | void add(Highscore &score); 62 | 63 | bool isHighscore(long score); 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /fx.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fx.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-13. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __FX__H__ 29 | #define __FX__H__ 30 | 31 | #include "SDL.h" 32 | 33 | class Fx 34 | { 35 | private: 36 | double _to, _from; 37 | double _value; 38 | 39 | double _duration; 40 | Uint32 _start_time; 41 | double(*_transition)(double); 42 | bool _is_running; 43 | public: 44 | enum Mode {Single, Tween}; 45 | Mode _mode; 46 | 47 | Fx(double(*trans)(double)); 48 | Fx(): _is_running(false) {}; 49 | 50 | void transition(double(*trans)(double)) { _transition = trans; }; 51 | void mode(Mode m) { _mode = m; }; 52 | void value(double from, double to) { _from = from; _to = to; }; 53 | void value(double value) { _value = value; }; 54 | 55 | void duration(double duration) { _duration = duration; }; 56 | bool isRunning() { return _is_running; }; 57 | 58 | void start(); 59 | void stop(); 60 | double step(); 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /menu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * menu.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-21. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __MENU_H__ 29 | #define __MENU_H__ 30 | 31 | #include 32 | 33 | #include "SDL.h" 34 | 35 | #include "highscoremanager.h" 36 | 37 | #include "game.h" 38 | #include "sprite.h" 39 | 40 | class Screen; 41 | 42 | const int NUM_SCREENS = 3; 43 | 44 | class Menu: public Sprite 45 | { 46 | private: 47 | SDL_Surface *_overlay; 48 | Screen *_screens[3]; 49 | Screen *_current_screen; 50 | public: 51 | enum MenuScreen {Home, Submit, Settings}; 52 | 53 | Menu(Game *game); 54 | void tick(); 55 | void draw(); 56 | void handleEvent(const SDL_Event &event); 57 | 58 | void showScreen(MenuScreen screen); 59 | 60 | int centerX() { return _game->size()->w / 2; }; 61 | int centerY() { return _game->size()->h / 2; }; 62 | 63 | SDL_Surface *overlay() { return _overlay; }; 64 | 65 | HighscoreManager manager; 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /scorescrollnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * scorescrollnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __SCORESCROLLNODE_H__ 29 | #define __SCORESCROLLNODE_H__ 30 | 31 | #include 32 | 33 | #include "SDL.h" 34 | #include "SDL/SDL_ttf.h" 35 | 36 | #include "highscoremanager.h" 37 | 38 | #include "node.h" 39 | 40 | class Screen; 41 | 42 | class ScoreScrollNode: public Node 43 | { 44 | private: 45 | typedef std::vector score_triplet_list; 46 | 47 | score_triplet_list _scores; 48 | 49 | TTF_Font *_font; 50 | SDL_Color _name_color; 51 | SDL_Color _score_color; 52 | SDL_Color _bg_color; 53 | 54 | int _font_height, _height; 55 | 56 | SDL_Surface *_image; 57 | SDL_Rect _scroll_rect, _rect; 58 | public: 59 | ScoreScrollNode(Screen *); 60 | void draw(SDL_Surface *); 61 | void renderText(); 62 | 63 | void onshow(); 64 | void ondrag(int relx, int rely); 65 | void onscroll(bool up); 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /node.h: -------------------------------------------------------------------------------- 1 | /* 2 | * node.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __NODE_H__ 29 | #define __NODE_H__ 30 | 31 | #include 32 | #include "SDL.h" 33 | 34 | class Screen; 35 | 36 | class Node 37 | { 38 | protected: 39 | SDL_Surface *_image; 40 | SDL_Rect _pos, _scroll; 41 | 42 | bool _is_focused; 43 | 44 | Screen *_screen; 45 | public: 46 | Node(Screen *screen); 47 | 48 | // Draw myself onto surface 49 | virtual void draw(SDL_Surface *surface); 50 | 51 | void focus(bool is) { _is_focused = is; }; 52 | 53 | bool collidesWith(int x, int y) { return x > _pos.x && y > _pos.y && x < _pos.x + _pos.w && y < _pos.y + _pos.h; }; 54 | 55 | // Events 56 | virtual void onclick() {}; 57 | virtual void onhover() {}; 58 | virtual void ontype(Uint16 key, SDLKey k) {}; 59 | virtual void onshow() {}; 60 | virtual void ondrag(int relx, int rely) {}; 61 | virtual void onscroll(bool up) {}; 62 | virtual void onfocus() {}; 63 | virtual void onblur() {}; 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /screen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * screen.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __SCREEN_H__ 29 | #define __SCREEN_H__ 30 | 31 | #include 32 | #include "SDL.h" 33 | 34 | class Node; 35 | class Menu; 36 | class Game; 37 | 38 | class Screen 39 | { 40 | protected: 41 | SDL_Rect _pos; 42 | 43 | typedef std::list node_list; 44 | node_list _nodes; 45 | 46 | Node *_focus_node; 47 | Menu *_menu; 48 | 49 | bool _is_dragging; 50 | public: 51 | enum Events {Click, Hover, Type, Scroll, Show, Down, Move}; 52 | 53 | Screen(Menu *menu): _menu(menu), _focus_node(NULL), _is_dragging(false) {}; 54 | ~Screen(); 55 | 56 | void addNode(Node *node); 57 | virtual void draw(SDL_Surface *) = 0; 58 | void drawNodes(SDL_Surface *); 59 | 60 | SDL_Rect makeRect(SDL_Rect r); 61 | 62 | void fireEvent(Events type, const SDL_Event &event); 63 | void fireEvent(Events type); 64 | Node *getNodeOn(int x, int y); 65 | 66 | Game *game(); 67 | Menu *menu() { return _menu; } 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /abstract_inputnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * abstract_inputnode.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __ABSTRACT_INPUTNODE_H__ 29 | #define __ABSTRACT_INPUTNODE_H__ 30 | 31 | #include "node.h" 32 | 33 | class Screen; 34 | 35 | namespace Abstract 36 | { 37 | class InputNode: public Node 38 | { 39 | private: 40 | SDL_Surface *_cursor; 41 | SDL_Rect _cursor_rect; 42 | 43 | TTF_Font *_font; 44 | SDL_Color _foreground; 45 | 46 | int _curr_index; 47 | 48 | void renderText(); 49 | protected: 50 | Uint16 _str[40]; 51 | 52 | void init(); 53 | void addChar(Uint16 code, SDLKey k); 54 | void loadFont(int size, Uint16 r, Uint16 g, Uint16 b); 55 | public: 56 | InputNode(Screen *screen); 57 | void draw(SDL_Surface *); 58 | 59 | // Methods that can be overwritten 60 | void onfocus() {}; 61 | void onblur() {}; 62 | void submit() {}; 63 | void onclick() {}; 64 | void ontype(Uint16, SDLKey) {}; 65 | 66 | // String handling 67 | void str(const char* s); 68 | void empty(); 69 | }; 70 | } 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /unicode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * unicode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-02-16. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include "unicode.h" 30 | 31 | using std::cout; 32 | using std::endl; 33 | 34 | int uni_length(const Uint16 *text) 35 | { 36 | for ( int i = 0; true; i++ ) 37 | if ( text[i] == '\0' ) 38 | return i; 39 | 40 | return sizeof(text) - 1; 41 | } 42 | 43 | void uni_place(Uint16 *text, Uint16 ch, int arr_size, int index) 44 | { 45 | if ( index >= arr_size - 1 || index < 0 ) 46 | return; 47 | 48 | // Move everything back one step 49 | for ( int i = arr_size - 3; i >= index; i-- ) 50 | text[i + 1] = text[i]; 51 | 52 | // Add back removed null byte character from end 53 | text[arr_size - 1] = '\0'; 54 | 55 | // Insert new character 56 | text[index] = ch; 57 | } 58 | 59 | void uni_slice(Uint16 *text, int arr_size, int index) 60 | { 61 | if ( index >= arr_size - 1 || index < 0 ) 62 | return; 63 | 64 | for ( int i = index; i < arr_size - 1; i++ ) 65 | { 66 | text[i] = text[i + 1]; 67 | } 68 | text[arr_size - 1] = '\0'; 69 | } 70 | -------------------------------------------------------------------------------- /fx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * fx.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-13. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include "SDL.h" 30 | 31 | #include "fx.h" 32 | 33 | Fx::Fx(double(*trans)(double)): _transition(trans), _is_running(false) 34 | { 35 | 36 | } 37 | 38 | void Fx::start() 39 | { 40 | _start_time = SDL_GetTicks(); 41 | _is_running = true; 42 | } 43 | 44 | double Fx::step() 45 | { 46 | Uint32 time = SDL_GetTicks(); 47 | double time_delta = (time - _start_time) / _duration; 48 | if ( _mode == Tween ) 49 | { 50 | if ( time_delta >= 1 ) 51 | { 52 | stop(); 53 | return _to; 54 | } 55 | double delta = _transition(time_delta); 56 | return (_to - _from) * delta + _from; 57 | } 58 | else if ( _mode == Single ) 59 | { 60 | if ( time_delta >= 1 ) 61 | { 62 | stop(); 63 | return _value * _transition(1); 64 | } 65 | return _value * _transition(time_delta); 66 | } 67 | return -1; 68 | } 69 | 70 | void Fx::stop() 71 | { 72 | _is_running = false; 73 | } 74 | 75 | /*void foo() 76 | { 77 | Fx anim(Fx::pow); 78 | anim.value(100, 200); 79 | anim.duration(500); 80 | 81 | anim.start(); 82 | double value = anim.step(); 83 | 84 | std::cout << value << std::endl; 85 | }*/ 86 | -------------------------------------------------------------------------------- /arrow.h: -------------------------------------------------------------------------------- 1 | /* 2 | * arrow.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-22. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __ARROW_H__ 29 | #define __ARROW_H__ 30 | 31 | #include 32 | 33 | #include "SDL.h" 34 | 35 | #include "sprite.h" 36 | #include "vector.h" 37 | #include "ball.h" 38 | 39 | const int ARROW_QUEUE_SIZE = 5; 40 | const Uint8 ARROW_BALL_OPACITY = 0x99; 41 | 42 | class Arrow: public Sprite 43 | { 44 | static const double MAX_ANGLE = 170; 45 | static const double MIN_ANGLE = 10; 46 | 47 | typedef std::list ball_queue; 48 | ball_queue _queue; 49 | 50 | Ball *_current_ball; 51 | 52 | SDL_Surface *_origin; 53 | SDL_Rect _pos; 54 | Vector _mouse_pos; 55 | double _angle; 56 | 57 | bool _is_ready; 58 | public: 59 | Arrow(Game *game); 60 | ~Arrow(); 61 | 62 | void prepareQueue(); 63 | 64 | void tick(); 65 | void draw(); 66 | void handleEvent(const SDL_Event &event); 67 | 68 | void rotateToMouse(); 69 | 70 | void checkQueueColors(); 71 | void setCurrent(bool is_in_game = false); 72 | void release(); 73 | 74 | Ball* createBall(); 75 | Ball* current() { return _current_ball; }; 76 | 77 | bool isReady() const { return _is_ready; }; 78 | void setReady(bool ready) { _is_ready = ready; }; 79 | }; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /sound.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * sound.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "SDL_mixer.h" 31 | #include "sound.h" 32 | 33 | void play_music() 34 | { 35 | static Mix_Music *sound = Mix_LoadMUS("kattMickisShooter.mp3"); 36 | if (sound == NULL) 37 | { 38 | std::cout << Mix_GetError() << std::endl; 39 | sound = Mix_LoadMUS("kattMickisShooter.ogg"); 40 | } 41 | if (sound == NULL) 42 | std::cout << Mix_GetError() << std::endl; 43 | Mix_PlayMusic(sound, -1); 44 | } 45 | 46 | void play_ball_bounce() 47 | { 48 | static Mix_Chunk *sound = Mix_LoadWAV("bing.wav"); 49 | Mix_PlayChannel(-1, sound, 0); 50 | } 51 | 52 | void play_ball_thud() 53 | { 54 | static Mix_Chunk *sound = Mix_LoadWAV("thud.wav"); 55 | Mix_PlayChannel(-1, sound, 0); 56 | } 57 | 58 | void play_ball_die() 59 | { 60 | static Mix_Chunk *sound = Mix_LoadWAV("die.wav"); 61 | Mix_PlayChannel(-1, sound, 0); 62 | } 63 | 64 | void play_lose() 65 | { 66 | static Mix_Chunk *sound = Mix_LoadWAV("no.wav"); 67 | Mix_PlayChannel(-1, sound, 0); 68 | } 69 | 70 | void play_win() 71 | { 72 | static Mix_Chunk *sound = Mix_LoadWAV("hurray.wav"); 73 | if ( ! sound ) 74 | std::cout << Mix_GetError(); 75 | Mix_PlayChannel(-1, sound, 0); 76 | } -------------------------------------------------------------------------------- /inputnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * inputnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "SDL.h" 31 | #include "SDL/SDL_ttf.h" 32 | 33 | #include "mysdl.h" 34 | #include "unicode.h" 35 | #include "highscoremanager.h" 36 | 37 | #include "highscorescreen.h" 38 | 39 | #include "game.h" 40 | #include "menu.h" 41 | #include "screen.h" 42 | #include "inputnode.h" 43 | 44 | InputNode::InputNode(Screen *screen): Abstract::InputNode(screen) 45 | { 46 | // Load font 47 | loadFont(16, 54, 64, 66); 48 | 49 | // (Input field) Position is everything 50 | _pos.x = 2 + 5; 51 | _pos.y = 71 + 6; 52 | _pos.w = 132; 53 | _pos.h = 31; 54 | 55 | str("Your name..."); 56 | 57 | init(); 58 | } 59 | 60 | void InputNode::onclick() 61 | { 62 | empty(); 63 | } 64 | 65 | void InputNode::ontype(Uint16 code, SDLKey k) 66 | { 67 | addChar(code, k); 68 | ((HighscoreScreen*)_screen)->name(_str); 69 | 70 | if ( k == SDLK_RETURN ) 71 | { 72 | submit(); 73 | } 74 | } 75 | 76 | void InputNode::onfocus() 77 | { 78 | _screen->menu()->game()->holdKeyEvents(true); 79 | } 80 | 81 | void InputNode::onblur() 82 | { 83 | _screen->menu()->game()->holdKeyEvents(false); 84 | } 85 | 86 | void InputNode::submit() 87 | { 88 | ((HighscoreScreen*)_screen)->submit(); 89 | } 90 | -------------------------------------------------------------------------------- /points.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * points.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-20. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | #include "mysdl.h" 32 | #include "game.h" 33 | #include "points.h" 34 | 35 | Points::Points(Game *game): Sprite(game), _last_points(0) 36 | { 37 | _font = load_font(20); 38 | 39 | _font_color.r = _font_color.g = _font_color.b = 0xFF; 40 | 41 | renderPoints(0); 42 | } 43 | 44 | Points::~Points() 45 | { 46 | SDL_FreeSurface(_image); 47 | } 48 | 49 | void Points::renderPoints(long points) 50 | { 51 | _last_points = points; 52 | 53 | std::stringstream pstr; 54 | pstr << _last_points << " points"; 55 | 56 | // Clear old image 57 | if ( _image ) 58 | SDL_FreeSurface(_image); 59 | 60 | _image = TTF_RenderText_Blended(_font, pstr.str().c_str(), _font_color); 61 | 62 | _rect = _image->clip_rect; 63 | _rect.x = _game->size()->w - _rect.w - 40; 64 | _rect.y = _game->size()->h - _rect.h - 10; 65 | } 66 | 67 | void Points::tick() 68 | { 69 | if ( _game->isPaused() ) 70 | return; 71 | 72 | long points = _game->points(); 73 | if ( _last_points != points ) 74 | { 75 | renderPoints(points); 76 | } 77 | } 78 | 79 | void Points::draw() 80 | { 81 | if ( _game->isPaused() ) 82 | return; 83 | 84 | SDL_BlitSurface(_image, NULL, _game->buffer(), &_rect); 85 | } 86 | -------------------------------------------------------------------------------- /vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * vector.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-30. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __VECTOR_H__ 29 | #define __VECTOR_H__ 30 | 31 | #include 32 | #include "SDL.h" 33 | 34 | class Vector 35 | { 36 | public: 37 | double _x, _y; 38 | 39 | Vector() {} 40 | Vector(double x, double y): _x(x), _y(y) {} 41 | Vector(const Vector &vec): _x(vec.x()), _y(vec.y()) {} 42 | 43 | double x() const { return _x; } 44 | double y() const { return _y; } 45 | 46 | void x(double x) { _x = x; } 47 | void y(double y) { _y = y; } 48 | 49 | Vector &operator=(Vector vec); 50 | bool operator==(Vector vec); 51 | bool operator!=(Vector vec); 52 | Vector &operator+=(Vector vec); 53 | Vector operator+(Vector vec); 54 | Vector &operator-=(Vector vec); 55 | Vector operator-(Vector vec); 56 | Vector &operator-(); 57 | Vector operator*(double scale); 58 | Vector &operator*=(double scale); 59 | 60 | double dot(Vector &vec); 61 | 62 | double length() const; 63 | double angle() const; 64 | void angle(double deg); 65 | double angleBetween(Vector &vec); 66 | double angleSeparating(Vector &vec); 67 | double distanceBetween(Vector &vec); 68 | 69 | SDL_Rect toRect(); 70 | void visualize(SDL_Surface *dst); 71 | 72 | void zero() { _x = _y = 0; }; 73 | bool isZero() { return _x == 0 && _y == 0; }; 74 | 75 | friend std::ostream & operator<<(std::ostream &os, const Vector &V); 76 | }; 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /music.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * music.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-08-17. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "music.h" 30 | 31 | #include "SDL.h" 32 | #include "SDL_image.h" 33 | 34 | #include "lib.h" 35 | 36 | Music::Music(Game *game): Sprite(game) 37 | { 38 | _is_playing = Lib::loadImage("sound.png"); 39 | _is_mute = Lib::loadImage("mute.png"); 40 | 41 | _image = _game->isPlayingMusic() ? _is_playing : _is_mute; 42 | 43 | _default_pos.x = _game->size()->w - _is_playing->clip_rect.w - 10; 44 | _default_pos.y = _game->size()->h - _is_playing->clip_rect.h - 10; 45 | 46 | _paused_pos.x = _game->size()->w - _is_playing->clip_rect.w - 10; 47 | _paused_pos.y = 10; 48 | 49 | _rect.x = _default_pos.x; 50 | _rect.y = _default_pos.y; 51 | } 52 | 53 | void Music::tick() 54 | { 55 | _image = _game->isPlayingMusic() ? _is_playing : _is_mute; 56 | 57 | if ( _game->isPaused() ) 58 | { 59 | _rect.x = _paused_pos.x; 60 | _rect.y = _paused_pos.y; 61 | } 62 | else 63 | { 64 | _rect.x = _default_pos.x; 65 | _rect.y = _default_pos.y; 66 | } 67 | 68 | } 69 | 70 | void Music::draw() 71 | { 72 | SDL_BlitSurface(_image, NULL, _game->buffer(), &_rect); 73 | } 74 | 75 | void Music::handleEvent(const SDL_Event &event) 76 | { 77 | switch (event.type) { 78 | case SDL_MOUSEBUTTONDOWN: 79 | if ( collidesWith(event.button.x, event.button.y) ) { 80 | _game->toggleMusic(); 81 | } 82 | break; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sprite.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-18. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. * 25 | */ 26 | 27 | #ifndef __DRAWABLE_H__ 28 | #define __DRAWABLE_H__ 29 | 30 | #include 31 | #include "SDL.h" 32 | 33 | class Game; 34 | 35 | class Sprite 36 | { 37 | protected: 38 | static int CID; 39 | 40 | Game *_game; 41 | bool _is_dirty; 42 | bool _is_visible; 43 | 44 | SDL_Surface *_image, *_old_image; 45 | SDL_Rect _rect; 46 | SDL_Rect _old_rect; 47 | 48 | void setImage(SDL_Surface *surface); 49 | public: 50 | int ID; 51 | 52 | Sprite() {}; 53 | Sprite(Game *game): _game(game), _is_dirty(true), _is_visible(true), _image(NULL) 54 | { 55 | ID = CID++; 56 | }; 57 | 58 | virtual void handleEvent(const SDL_Event &event) {}; 59 | virtual void tick() = 0; 60 | virtual void draw() = 0; 61 | 62 | void preTick(); 63 | void postTick(); 64 | 65 | inline void dirty(bool is) { _is_dirty = is; }; 66 | inline bool dirty() const { return _is_dirty; }; 67 | 68 | inline void visible(bool is) { _is_visible = is; }; 69 | inline bool visible() { return _is_visible; }; 70 | 71 | Game *game() { return _game; }; 72 | 73 | bool collidesWith(SDL_Rect r) 74 | { 75 | // right inside left top inside bottom left inside right bottom inside top 76 | return r.x + r.w > _rect.x && r.y < _rect.y + _rect.h && r.x < _rect.x + _rect.w && r.y + r.h > _rect.y; 77 | } 78 | 79 | bool collidesWith(int x, int y) 80 | { 81 | return x > _rect.x && y > _rect.y && x < _rect.x + _rect.w && y < _rect.y + _rect.h; 82 | } 83 | }; 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /grid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * grid.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-11. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __GRID_H__ 29 | #define __GRID_H__ 30 | 31 | #include 32 | 33 | #include "ball.h" 34 | #include "sprite.h" 35 | 36 | // This class will be used by the search-part of Grid 37 | class GridPos 38 | { 39 | public: 40 | GridPos(int gx, int gy): x(gx), y(gy) {}; 41 | int x, y; 42 | }; 43 | 44 | typedef std::list ball_list; 45 | 46 | class Grid: public Sprite 47 | { 48 | private: 49 | typedef std::list grid_list; 50 | 51 | ball_list _balls; 52 | ball_list _remove_list; 53 | 54 | Ball *_remove_top; 55 | 56 | public: 57 | Grid(Game *game); 58 | void tick(); 59 | void draw(); 60 | 61 | // Row handling 62 | void removeBall(Ball *ball); 63 | void addBall(Ball *ball); 64 | void removeBalls(ball_list &balls); 65 | void generateRow(int rows) { generateRow(rows, false); }; 66 | void generateRow(int rows, bool is_startrow); 67 | void generateStartRows(); 68 | int countRows(); 69 | void emptyRows(); 70 | 71 | // Ball detection 72 | Ball *inCollision(Ball &); 73 | bool hasBallOn(int x, int y); 74 | Ball *getBallOn(int x, int y); 75 | Ball *getBallWithColor(int x, int y, Ball::Colors color); 76 | 77 | // Group handling 78 | void locateGroups(Ball &relativeTo); 79 | bool gridListHas(grid_list &, int, int); 80 | void search(Ball &ball, ball_list &found); 81 | void handleDanglies(); 82 | bool doSearch(Ball &ball, ball_list &found, grid_list &searched, bool recursive, bool use_color); 83 | 84 | ball_list& balls() { return _balls; }; 85 | }; 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /debug.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * debug.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-18. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include "debug.h" 30 | 31 | void print_pixel_at(const char *name, SDL_Surface *surface, int x, int y) 32 | { 33 | /* Extracting color components from a 32-bit color value */ 34 | SDL_PixelFormat *fmt; 35 | Uint32 temp, pixel; 36 | Uint8 red, green, blue, alpha; 37 | 38 | fmt = surface->format; 39 | SDL_LockSurface(surface); 40 | pixel = *((Uint32*)surface->pixels) + x + y; 41 | SDL_UnlockSurface(surface); 42 | 43 | /* Get Red component */ 44 | temp = pixel & fmt->Rmask; /* Isolate red component */ 45 | temp = temp >> fmt->Rshift; /* Shift it down to 8-bit */ 46 | temp = temp << fmt->Rloss; /* Expand to a full 8-bit number */ 47 | red = (Uint8)temp; 48 | 49 | /* Get Green component */ 50 | temp = pixel & fmt->Gmask; /* Isolate green component */ 51 | temp = temp >> fmt->Gshift; /* Shift it down to 8-bit */ 52 | temp = temp << fmt->Gloss; /* Expand to a full 8-bit number */ 53 | green = (Uint8)temp; 54 | 55 | /* Get Blue component */ 56 | temp = pixel & fmt->Bmask; /* Isolate blue component */ 57 | temp = temp >> fmt->Bshift; /* Shift it down to 8-bit */ 58 | temp = temp << fmt->Bloss; /* Expand to a full 8-bit number */ 59 | blue = (Uint8)temp; 60 | 61 | /* Get Alpha component */ 62 | temp = pixel & fmt->Amask; /* Isolate alpha component */ 63 | temp = temp >> fmt->Ashift; /* Shift it down to 8-bit */ 64 | temp = temp << fmt->Aloss; /* Expand to a full 8-bit number */ 65 | alpha = (Uint8)temp; 66 | 67 | printf("Pixel Color (%s) -> R: %d, G: %d, B: %d, A: %d\n", name, red, green, blue, alpha); 68 | } 69 | 70 | std::ostream & operator<<(std::ostream &os, const SDL_Rect &r ) 71 | { 72 | os << "[SDL_Rect left: " << r.x << ", top: " << r.y << ", right: " << r.x + r.w << ", bottom: " << r.y + r.h << ", w: " << r.w << ", h: " << r.h << "]\n"; 73 | return os; 74 | } 75 | -------------------------------------------------------------------------------- /highscoremanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * highscoremanager.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "highscoremanager.h" 34 | 35 | using std::fstream; 36 | using std::ofstream; 37 | 38 | HighscoreManager::HighscoreManager() {} 39 | 40 | void HighscoreManager::load() 41 | { 42 | // First we clear all old scores 43 | for ( highscore_list::iterator iter = _scores.begin(); iter != _scores.end(); iter++ ) 44 | delete (*iter); 45 | 46 | fstream fs("scores", fstream::in); 47 | 48 | if ( fs.is_open() ) 49 | { 50 | Highscore hs, *hsobj; 51 | while ( fs.read((char *) &hs, sizeof hs) ) 52 | { 53 | hsobj = new Highscore; 54 | *hsobj = hs; 55 | _scores.push_back(hsobj); 56 | } 57 | 58 | fs.close(); 59 | } 60 | 61 | _scores.sort(comp_highscore); 62 | } 63 | 64 | void HighscoreManager::save() 65 | { 66 | ofstream fs("scores", fstream::trunc); 67 | 68 | if ( fs.is_open() ) 69 | { 70 | for ( highscore_list::iterator iter = _scores.begin(); iter != _scores.end(); iter++ ) 71 | { 72 | Highscore hs = (**iter); 73 | fs.write((char *) &hs, sizeof hs); 74 | } 75 | } 76 | 77 | fs.close(); 78 | } 79 | 80 | void HighscoreManager::add(Highscore &score) 81 | { 82 | Highscore *s = new Highscore; 83 | *s = score; 84 | 85 | _scores.push_back(s); 86 | 87 | _scores.sort(comp_highscore); 88 | 89 | if ( _scores.size() >= HIGHSCORES_SAVED ) 90 | { 91 | // Remove last score 92 | Highscore *old = _scores.back(); 93 | _scores.pop_back(); 94 | 95 | delete old; 96 | } 97 | } 98 | 99 | bool HighscoreManager::isHighscore(long score) 100 | { 101 | if ( _scores.size() < HIGHSCORES_SAVED ) 102 | return true; 103 | 104 | _scores.sort(comp_highscore); 105 | highscore_list::iterator iter = _scores.begin(); 106 | advance(iter, HIGHSCORES_SAVED - 1); // Go to last score 107 | 108 | if ( score > (*iter)->score ) 109 | return true; 110 | return false; 111 | } 112 | -------------------------------------------------------------------------------- /menu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * menu.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-21. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "SDL.h" 29 | #include "SDL_image.h" 30 | 31 | #include "mysdl.h" 32 | #include "homescreen.h" 33 | #include "highscorescreen.h" 34 | #include "settingscreen.h" 35 | #include "highscoremanager.h" 36 | 37 | #include "game.h" 38 | #include "screen.h" 39 | #include "menu.h" 40 | 41 | Menu::Menu(Game *game): Sprite(game), _current_screen(NULL) 42 | { 43 | // Initialize overlay 44 | _overlay = IMG_Load("menu.png"); 45 | 46 | // Add screens 47 | _screens[Home] = new HomeScreen(this); 48 | _screens[Submit] = new HighscoreScreen(this); 49 | _screens[Settings] = new SettingsScreen(this); 50 | 51 | _current_screen = _screens[Home]; 52 | 53 | manager.load(); 54 | highscore_list scores = manager.get(); 55 | } 56 | 57 | void Menu::tick() {} 58 | 59 | void Menu::draw() 60 | { 61 | if ( ! _game->isPaused() ) 62 | return; 63 | 64 | // Draw overlay 65 | SDL_BlitSurface(_overlay, NULL, _game->buffer(), NULL); 66 | 67 | // Draw screens 68 | if ( _current_screen ) 69 | _current_screen->draw(_game->buffer()); 70 | } 71 | 72 | void Menu::handleEvent(const SDL_Event &event) 73 | { 74 | if ( ! _game->isPaused() || ! _current_screen ) 75 | return; 76 | 77 | _game->markForRedraw(); 78 | 79 | switch (event.type) 80 | { 81 | case SDL_MOUSEBUTTONDOWN: 82 | if ( event.button.button != SDL_BUTTON_WHEELDOWN && event.button.button != SDL_BUTTON_WHEELUP ) 83 | _current_screen->fireEvent(Screen::Down, event); 84 | else 85 | _current_screen->fireEvent(Screen::Scroll, event); 86 | 87 | break; 88 | case SDL_MOUSEMOTION: 89 | _current_screen->fireEvent(Screen::Move, event); 90 | break; 91 | 92 | case SDL_MOUSEBUTTONUP: 93 | _current_screen->fireEvent(Screen::Click, event); 94 | break; 95 | 96 | case SDL_KEYDOWN: 97 | _current_screen->fireEvent(Screen::Type, event); 98 | break; 99 | } 100 | } 101 | 102 | void Menu::showScreen(MenuScreen menu) 103 | { 104 | if ( menu == Submit ) 105 | { 106 | if ( ! manager.isHighscore(_game->lastPoints()) ) 107 | menu = Home; 108 | } 109 | 110 | _current_screen = _screens[menu]; 111 | _current_screen->fireEvent(Screen::Show); 112 | 113 | if ( menu == Submit ) 114 | { 115 | ((HighscoreScreen*)_current_screen)->renderPointsText(); 116 | } 117 | 118 | _game->markForRedraw(); 119 | } 120 | -------------------------------------------------------------------------------- /highscorescreen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * highscorescreen.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "SDL.h" 31 | #include "SDL_image.h" 32 | #include "SDL/SDL_ttf.h" 33 | 34 | #include "mysdl.h" 35 | 36 | // Nodes 37 | #include "inputnode.h" 38 | #include "cancelnode.h" 39 | #include "submitnode.h" 40 | #include "highscorenode.h" 41 | 42 | #include "highscorescreen.h" 43 | 44 | HighscoreScreen::HighscoreScreen(Menu *menu): Screen(menu), _points(NULL) 45 | { 46 | for ( int i = 0; i < 40; i++ ) 47 | _name[i] = '\0'; 48 | 49 | _image = IMG_Load("highscore.png"); 50 | 51 | // My position on the screen 52 | _pos.w = 308; 53 | _pos.h = 170; 54 | _pos.x = _menu->centerX() - _pos.w / 2; 55 | _pos.y = _menu->centerY() - _pos.h / 2 + 20; 56 | 57 | // Controls position relative to me 58 | // Perhaps this and _text should be nodes 59 | _controls.x = 0 + _pos.x; 60 | _controls.y = 70 + _pos.y; 61 | 62 | _fg.r = _fg.g = _fg.b = 0xFF; 63 | 64 | // Create Wow! You got... text 65 | TTF_Font *font = load_font(16); 66 | _text = TTF_RenderText_Blended(font, "Wow! You got:", _fg); 67 | TTF_CloseFont(font); 68 | 69 | _text_rect.x = 0 + _pos.x; 70 | _text_rect.y = 10 + _pos.y; 71 | 72 | // Initialize X points 73 | _font = load_font(20); 74 | renderPointsText(); 75 | 76 | addNode(new InputNode(this)); 77 | addNode(new CancelNode(this)); 78 | addNode(new SubmitNode(this)); 79 | addNode(new HighscoreNode(this)); 80 | } 81 | 82 | void HighscoreScreen::draw(SDL_Surface *surface) 83 | { 84 | SDL_BlitSurface(_image, NULL, surface, &_controls); 85 | SDL_BlitSurface(_text, NULL, surface, &_text_rect); 86 | SDL_BlitSurface(_points, NULL, surface, &_points_rect); 87 | 88 | drawNodes(surface); 89 | } 90 | 91 | void HighscoreScreen::renderPointsText() 92 | { 93 | if ( _points ) 94 | SDL_FreeSurface(_points); 95 | 96 | std::stringstream text; 97 | text << _menu->game()->lastPoints(); 98 | text << " points!"; 99 | _points = TTF_RenderText_Blended(_font, text.str().c_str(), _fg); 100 | 101 | _points_rect = _text_rect; 102 | _points_rect.y += _text->clip_rect.h; 103 | } 104 | 105 | void HighscoreScreen::submit() 106 | { 107 | if ( _name[0] == '\0' ) 108 | return; 109 | 110 | Highscore hs; 111 | for ( int i = 0; i < 40; i++ ) 112 | hs.name[i] = _name[i]; 113 | hs.score = game()->lastPoints(); 114 | menu()->manager.add(hs); 115 | menu()->manager.save(); 116 | 117 | menu()->showScreen(Menu::Home); 118 | } 119 | -------------------------------------------------------------------------------- /ball.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ball.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-07. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __BALL_H__ 29 | #define __BALL_H__ 30 | 31 | #include 32 | 33 | #include "sprite.h" 34 | #include "vector.h" 35 | #include "fx.h" 36 | 37 | const int BALL_GRID_W = 17; 38 | const int BALL_GRID_H = 15; 39 | 40 | const int BALL_WIDTH = 23; 41 | const int BALL_HEIGHT = 23; 42 | 43 | class Ball: public Sprite 44 | { 45 | private: 46 | // _sprite is the original ball image, not changed by animations. 47 | SDL_Surface *_sprite; 48 | 49 | Vector _pos; 50 | 51 | int _grid_x, _grid_y; 52 | 53 | Uint8 _opacity; 54 | bool _was_dangly; 55 | 56 | void initFx(); 57 | public: 58 | enum BallState {Queued, Moving, Pinned}; 59 | BallState _state; 60 | 61 | static Ball *create(Game *game); 62 | 63 | Ball(Game *game); 64 | ~Ball(); 65 | 66 | enum Colors {Random, Red, Green, Yellow, Blue, Teal, Purple}; 67 | Colors _color; 68 | Colors color() { return _color; }; 69 | 70 | static const char* getC(Colors c) { 71 | switch (c) { 72 | case Red: return "Red"; break; 73 | case Green: return "Green"; break; 74 | case Yellow: return "Yellow"; break; 75 | case Blue: return "Blue"; break; 76 | case Teal: return "Teal"; break; 77 | case Purple: return "Purple"; break; 78 | case Random: return "Random!"; break; 79 | default: return "wtf"; break; 80 | } 81 | } 82 | 83 | static void printC(Colors c) { 84 | std::cout << getC(c); 85 | }; 86 | 87 | void setColor(Colors color); 88 | void setRemainingColor(); 89 | void ensureColorExists(); 90 | 91 | void setState(BallState state) { _state = state; }; 92 | 93 | void gridX(int x) { _grid_x = x; gridToPos(); }; 94 | void gridY(int y) { _grid_y = y; gridToPos(); }; 95 | 96 | int gridX() { return _grid_x; }; 97 | int gridY() { return _grid_y; }; 98 | 99 | Vector &pos() { return _pos; }; 100 | void xPos(double x) { _pos.x(x); _rect.x = x; }; 101 | void yPos(double y) { _pos.y(y); _rect.y = y; }; 102 | 103 | void wasDangly(bool was) { _was_dangly = was; }; 104 | bool wasDangly() { return _was_dangly; }; 105 | 106 | SDL_Rect *size() { return &_rect; }; 107 | 108 | void tick(); 109 | void draw(); 110 | 111 | Vector calculateGrid(); 112 | void satisfyGrid(); 113 | void gridToPos(); 114 | 115 | bool collidesWith(Ball &); 116 | 117 | void active(); 118 | void bam(); 119 | 120 | Vector _vel; 121 | Vector _acc; 122 | 123 | Fx _anim; 124 | }; 125 | 126 | double active_transition(double x); 127 | double bam_transition(double x); 128 | 129 | #endif 130 | -------------------------------------------------------------------------------- /mysdl.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * mysdl.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-19. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "mysdl.h" 31 | 32 | const bool USE_NATIVE_BLIT = false; 33 | 34 | void draw_transparent_surface_onto_empty_surface(SDL_Rect src_rect, SDL_Surface *empty, SDL_Surface *src) 35 | { 36 | if ( USE_NATIVE_BLIT ) 37 | { 38 | src->flags &= ~SDL_SRCALPHA; 39 | SDL_BlitSurface(src, &src_rect, empty, NULL); 40 | SDL_SetAlpha(src, SDL_SRCALPHA, 0xFF); 41 | } 42 | 43 | if ( ! USE_NATIVE_BLIT ) 44 | { 45 | SDL_Rect size = empty->clip_rect; 46 | int w = size.w, h = size.h; 47 | 48 | SDL_LockSurface(empty); 49 | SDL_LockSurface(src); 50 | 51 | int ebpp = empty->format->BytesPerPixel, 52 | sbpp = src->format->BytesPerPixel; 53 | 54 | Uint8 *pixel, *color; 55 | 56 | for ( int x = 0; x < w; x++ ) 57 | for ( int y = 0; y < h; y++ ) 58 | { 59 | pixel = (Uint8 *)empty->pixels + y * empty->pitch + x * ebpp; 60 | color = (Uint8 *)src->pixels + (y + src_rect.y) * src->pitch + (x + src_rect.x) * sbpp; 61 | 62 | *(Uint32*)pixel = *(Uint32*)color; 63 | } 64 | 65 | SDL_UnlockSurface(src); 66 | SDL_UnlockSurface(empty); 67 | } 68 | } 69 | 70 | SDL_Surface *change_opacity_of_surface(SDL_Surface *src, Uint8 opacity) 71 | { 72 | SDL_Surface *dst = SDL_DisplayFormatAlpha(src); 73 | 74 | int w = src->clip_rect.w, 75 | h = src->clip_rect.h; 76 | 77 | SDL_PixelFormat *fmt = src->format; 78 | Uint8 *pixel; 79 | Uint32 opacity_mask = (opacity << fmt->Ashift) & fmt->Amask | fmt->Rmask | fmt->Bmask | fmt->Gmask; 80 | 81 | SDL_LockSurface(src); 82 | SDL_LockSurface(dst); 83 | 84 | for ( int x = 0; x < w; x++ ) 85 | for ( int y = 0; y < h; y++ ) 86 | { 87 | pixel = (Uint8 *)dst->pixels + y * dst->pitch + x * fmt->BytesPerPixel; 88 | *(Uint32*)pixel = (*(Uint32*)pixel & opacity_mask); 89 | } 90 | 91 | SDL_UnlockSurface(dst); 92 | SDL_UnlockSurface(src); 93 | 94 | return dst; 95 | } 96 | 97 | bool check_rect_collision(SDL_Rect *rect, int x, int y) 98 | { 99 | return x > rect->x && y > rect->y && x < (rect->x + rect->w) && y < (rect->y + rect->h); 100 | } 101 | 102 | TTF_Font *load_font(int size) 103 | { 104 | TTF_Font *font = TTF_OpenFont("font.otf", size); 105 | if ( ! font ) 106 | { 107 | std::cerr << "Could not load font: " << TTF_GetError(); 108 | return NULL; 109 | } 110 | return font; 111 | } 112 | 113 | SDL_Rect get_rect_intersection(SDL_Rect one, SDL_Rect two) 114 | { 115 | SDL_Rect result; 116 | result.x = two.x - one.x; 117 | result.y = two.y - one.y; 118 | result.w = two.w - result.x; 119 | result.h = two.h - result.y; 120 | return result; 121 | } 122 | -------------------------------------------------------------------------------- /screen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * screen.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-23. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "menu.h" 29 | #include "node.h" 30 | #include "screen.h" 31 | 32 | Game *Screen::game() 33 | { 34 | return _menu->game(); 35 | } 36 | 37 | void Screen::addNode(Node *node) 38 | { 39 | _nodes.push_back(node); 40 | } 41 | 42 | void Screen::drawNodes(SDL_Surface *surface) 43 | { 44 | for ( node_list::iterator iter = _nodes.begin(); iter != _nodes.end(); iter++ ) 45 | (*iter)->draw(surface); 46 | } 47 | 48 | void Screen::fireEvent(Events type, const SDL_Event &event) 49 | { 50 | switch ( type ) 51 | { 52 | case Down: 53 | { 54 | // Get focused node 55 | Node *node = getNodeOn(event.button.x - _pos.x, event.button.y - _pos.y); 56 | 57 | // If it isn't the current node the old focused node is blurred 58 | if ( node != _focus_node && _focus_node != NULL ) 59 | { 60 | _focus_node->focus(false); 61 | _focus_node->onblur(); 62 | } 63 | // If focus_node is NULL empty space was clicked 64 | _focus_node = (node) ? node : NULL; 65 | // Focus event 66 | if ( _focus_node ) 67 | { 68 | _focus_node->focus(true); 69 | _focus_node->onfocus(); 70 | } 71 | 72 | _is_dragging = true; 73 | } 74 | break; 75 | 76 | case Scroll: 77 | if ( _focus_node ) 78 | _focus_node->onscroll(event.button.button == SDL_BUTTON_WHEELUP); 79 | break; 80 | 81 | case Move: 82 | if ( _is_dragging && _focus_node ) 83 | { 84 | _focus_node->ondrag(event.motion.xrel, event.motion.yrel); 85 | } 86 | break; 87 | 88 | case Type: 89 | if ( _focus_node ) 90 | { 91 | Uint16 key = event.key.keysym.unicode; 92 | _focus_node->ontype(key, event.key.keysym.sym); 93 | } 94 | break; 95 | 96 | case Click: 97 | if ( _focus_node ) 98 | _focus_node->onclick(); 99 | _is_dragging = false; 100 | break; 101 | } 102 | } 103 | 104 | void Screen::fireEvent(Events type) 105 | { 106 | switch ( type ) 107 | { 108 | case Show: 109 | _focus_node = NULL; 110 | for ( node_list::iterator iter = _nodes.begin(); iter != _nodes.end(); iter++) 111 | (*iter)->onshow(); 112 | break; 113 | } 114 | } 115 | 116 | Node *Screen::getNodeOn(int x, int y) 117 | { 118 | for ( node_list::iterator iter = _nodes.begin(); iter != _nodes.end(); iter++ ) 119 | if ( (*iter)->collidesWith(x, y) ) 120 | return (*iter); 121 | return NULL; 122 | } 123 | 124 | SDL_Rect Screen::makeRect(SDL_Rect r) 125 | { 126 | r.x += _pos.x; 127 | r.y += _pos.y; 128 | return r; 129 | } 130 | 131 | Screen::~Screen() 132 | { 133 | for ( node_list::iterator iter = _nodes.begin(); iter != _nodes.end(); iter++ ) 134 | delete (*iter); 135 | } 136 | -------------------------------------------------------------------------------- /vector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * vector.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-30. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | #include "vector.h" 32 | #include "SDL.h" 33 | 34 | const double PI = 3.14159262; 35 | const double RADTODEG = 180 / PI; 36 | const double DEGTORAD = PI / 180; 37 | 38 | Vector &Vector::operator=(Vector vec) 39 | { 40 | _x = vec.x(); 41 | _y = vec.y(); 42 | return *this; 43 | } 44 | 45 | bool Vector::operator==(Vector vec) 46 | { 47 | return _x == vec.x() && _y == vec.y(); 48 | } 49 | 50 | bool Vector::operator!=(Vector vec) 51 | { 52 | return _x != vec.x() || _y != vec.y(); 53 | } 54 | 55 | Vector &Vector::operator+=(Vector vec) 56 | { 57 | _x += vec.x(); 58 | _y += vec.y(); 59 | return *this; 60 | } 61 | 62 | Vector Vector::operator+(Vector vec) 63 | { 64 | Vector vecx = Vector(_x + vec.x(), _y + vec.y()); 65 | return Vector(_x + vec.x(), _y + vec.y()); 66 | } 67 | 68 | Vector &Vector::operator-=(Vector vec) 69 | { 70 | _x -= vec.x(); 71 | _y -= vec.y(); 72 | return *this; 73 | } 74 | 75 | Vector Vector::operator-(Vector vec) 76 | { 77 | return Vector(_x - vec.x(), _y - vec.y()); 78 | } 79 | 80 | Vector &Vector::operator*=(double scale) 81 | { 82 | _x *= scale; 83 | _y *= scale; 84 | return *this; 85 | } 86 | 87 | Vector Vector::operator*(double scale) 88 | { 89 | return Vector(_x * scale, _y * scale); 90 | } 91 | 92 | double Vector::dot(Vector &vec) 93 | { 94 | return _x * vec.x() + _y * vec.y(); 95 | } 96 | 97 | double Vector::length() const 98 | { 99 | return sqrt(_x * _x + _y * _y); 100 | } 101 | 102 | double Vector::angle() const 103 | { 104 | return atan2(_x, _y) * RADTODEG; 105 | } 106 | 107 | void Vector::angle(double deg) 108 | { 109 | double r = length(); 110 | 111 | _x = r * cos(deg * DEGTORAD); 112 | _y = r * sin(deg * DEGTORAD); 113 | } 114 | 115 | double Vector::angleBetween(Vector &vec) 116 | { 117 | double cosAngle = dot(vec) / (length() * vec.length()); 118 | return acos(cosAngle) * RADTODEG; 119 | } 120 | 121 | double Vector::angleSeparating(Vector &vec) 122 | { 123 | return (*this - vec).angle(); 124 | } 125 | 126 | double Vector::distanceBetween(Vector &vec) 127 | { 128 | return sqrt(pow(_x - vec.x(), 2) + pow(_y - vec.y(), 2)); 129 | } 130 | 131 | /* 132 | * Debuging 133 | */ 134 | 135 | SDL_Rect Vector::toRect() 136 | { 137 | SDL_Rect rect; 138 | rect.x = _x - 3; 139 | rect.y = _y - 3; 140 | rect.w = 6; 141 | rect.h = 6; 142 | return rect; 143 | } 144 | 145 | void Vector::visualize(SDL_Surface *dst) 146 | { 147 | SDL_Rect r = toRect(); 148 | SDL_FillRect(dst, &r, SDL_MapRGB(dst->format, 0xFF, 0x00, 0x00)); 149 | } 150 | 151 | std::ostream & operator<<(std::ostream &os, const Vector &V ) 152 | { 153 | os << "[" << V._x << ", " << V._y << "] (" << V.angle() << ", " << V.length() << ")"; 154 | return os; 155 | } 156 | -------------------------------------------------------------------------------- /ballmanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ballmanager.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-19. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "SDL_image.h" 33 | 34 | #include "ball.h" 35 | #include "mysdl.h" 36 | 37 | #include "ballmanager.h" 38 | 39 | SDL_Surface *BallManager::_image = NULL; 40 | SDL_Surface *BallManager::_balls[BALLMANAGER_COLORS] = {NULL, NULL, NULL, NULL, NULL, NULL}; 41 | std::vector BallManager::_available = std::vector(); 42 | 43 | SDL_Surface *BallManager::load(Ball::Colors color) 44 | { 45 | // Load first image 46 | if ( _image == NULL ) 47 | { 48 | SDL_Surface * image = IMG_Load("ball.png"); 49 | if ( ! image ) 50 | { 51 | std::cerr << "Could not load image: " << IMG_GetError() << std::endl; 52 | return NULL; 53 | } 54 | SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 0xFF, 0xFF, 0xFF)); 55 | 56 | _image = image; 57 | } 58 | 59 | // Create single image (if needed) 60 | int index = color - 1; 61 | if ( ! _balls[index] ) 62 | { 63 | // Make copy of the ball that we can enlarge 64 | SDL_PixelFormat *format = _image->format; 65 | SDL_Surface *ball = SDL_CreateRGBSurface(_image->flags, 66 | BALL_WIDTH, BALL_HEIGHT, format->BitsPerPixel, 67 | format->Rmask, format->Bmask, format->Gmask, format->Amask); 68 | // 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); 69 | //SDL_FillRect(ball, NULL, SDL_MapRGBA(format, 0x00, 0x00, 0x00, 0xFF)); 70 | 71 | SDL_Rect rect; 72 | rect.y = 0; 73 | switch ( color ) 74 | { 75 | case Ball::Red: rect.x = 0; break; 76 | case Ball::Green: rect.x = 23; break; 77 | case Ball::Yellow: rect.x = 46; break; 78 | case Ball::Blue: rect.x = 69; break; 79 | case Ball::Teal: rect.x = 92; break; 80 | case Ball::Purple: rect.x = 115; break; 81 | default: std::cout << "Sanity check failed" << std::endl; 82 | } 83 | 84 | draw_transparent_surface_onto_empty_surface(rect, ball, _image); 85 | 86 | _balls[index] = ball; 87 | } 88 | 89 | return _balls[index]; 90 | } 91 | 92 | Ball::Colors BallManager::randomColor() 93 | { 94 | Ball::Colors color; 95 | int random = rand() % 6 + 1; 96 | color = (Ball::Colors)random; 97 | return color; 98 | } 99 | 100 | void BallManager::prepRemList(Grid *grid) 101 | { 102 | // Clear old availables list 103 | _available.erase(_available.begin(), _available.end()); 104 | 105 | ball_list balls = grid->balls(); 106 | for ( ball_list::iterator iter = balls.begin(); iter != balls.end(); iter++ ) 107 | if ( count(_available.begin(), _available.end(), (*iter)->color()) == 0 ) 108 | _available.push_back((*iter)->color()); 109 | } 110 | 111 | Ball::Colors BallManager::randomRemainingColor(Grid *grid) 112 | { 113 | prepRemList(grid); 114 | 115 | std::random_shuffle(_available.begin(), _available.end()); 116 | return _available.front(); 117 | } 118 | 119 | bool BallManager::colorExists(Ball::Colors check) 120 | { 121 | return count(_available.begin(), _available.end(), check); 122 | } 123 | -------------------------------------------------------------------------------- /abstract_inputnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abstract_inputnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2010-03-04. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "SDL.h" 31 | #include "SDL/SDL_ttf.h" 32 | 33 | #include "mysdl.h" 34 | #include "unicode.h" 35 | #include "highscoremanager.h" 36 | 37 | #include "highscorescreen.h" 38 | 39 | #include "game.h" 40 | #include "menu.h" 41 | #include "screen.h" 42 | #include "inputnode.h" 43 | 44 | #include "abstract_inputnode.h" 45 | 46 | namespace Abstract 47 | { 48 | InputNode::InputNode(Screen *screen): Node(screen), _curr_index(0) 49 | { 50 | 51 | } 52 | 53 | // Init must be called after positions are set 54 | // This totally defeats the purpose of a constructor 55 | void InputNode::init() 56 | { 57 | // Create cursor 58 | _cursor = SDL_CreateRGBSurface(_screen->game()->buffer()->flags, 1, _pos.h - 11, _screen->game()->buffer()->format->BitsPerPixel, NULL, NULL, NULL, NULL); 59 | SDL_FillRect(_cursor, NULL, SDL_MapRGBA(_cursor->format, 0x66, 0x66, 0x66, 0xCC)); 60 | 61 | _cursor_rect = _cursor->clip_rect; 62 | _cursor_rect.y = 0; 63 | 64 | // Set scroll positions 65 | _scroll.w = _pos.w - 10; 66 | _scroll.h = _pos.h; 67 | } 68 | 69 | void InputNode::loadFont(int size, Uint16 r, Uint16 g, Uint16 b) 70 | { 71 | _font = load_font(size); 72 | _foreground.r = r; 73 | _foreground.g = g; 74 | _foreground.b = b; 75 | } 76 | 77 | void InputNode::draw(SDL_Surface *screen) 78 | { 79 | SDL_Rect rect = _screen->makeRect(_pos); 80 | SDL_BlitSurface(_image, &_scroll, screen, &rect); 81 | 82 | if ( ! _is_focused ) 83 | return; 84 | 85 | rect.x += _cursor_rect.x; 86 | rect.y += _cursor_rect.y; 87 | SDL_BlitSurface(_cursor, NULL, screen, &rect); 88 | } 89 | 90 | void InputNode::renderText() 91 | { 92 | if ( _image != NULL ) 93 | SDL_FreeSurface(_image); 94 | 95 | _image = TTF_RenderUNICODE_Blended(_font, _str, _foreground); 96 | 97 | // Account for input text scroll 98 | if ( _image != NULL && _image->clip_rect.w > _pos.w - 10 ) 99 | _scroll.x = _image->clip_rect.w - _pos.w + 10; 100 | else 101 | _scroll.x = 0; 102 | 103 | // Get width of the text string by placing 104 | // a null byte character up to the point we 105 | // want to count, then putting the original 106 | // back. Nice. 107 | int w; 108 | Uint16 t = _str[_curr_index]; 109 | _str[_curr_index] = '\0'; 110 | TTF_SizeUNICODE(_font, _str, &w, NULL); 111 | _str[_curr_index] = t; 112 | 113 | _cursor_rect.x = w - _scroll.x; 114 | 115 | if ( _cursor_rect.x < 0 ) 116 | { 117 | _scroll.x += _cursor_rect.x; 118 | _cursor_rect.x = 0; 119 | } 120 | } 121 | 122 | void InputNode::addChar(Uint16 code, SDLKey k) 123 | { 124 | Uint16 key = code; 125 | if ( k == SDLK_BACKSPACE ) 126 | { 127 | _curr_index--; 128 | if ( _curr_index >= 0 ) 129 | uni_slice(_str, 40, _curr_index); 130 | else 131 | _curr_index = 0; 132 | } 133 | else if ( k == SDLK_LEFT || k == SDLK_RIGHT || k == SDLK_UP || k == SDLK_DOWN ) 134 | { 135 | int len = uni_length(_str); 136 | 137 | if ( k == SDLK_LEFT ) 138 | _curr_index--; 139 | else if ( k == SDLK_RIGHT ) 140 | _curr_index++; 141 | else if ( k == SDLK_UP ) 142 | _curr_index = 0; 143 | else 144 | _curr_index = len; 145 | 146 | if ( _curr_index < 0 ) 147 | _curr_index = 0; 148 | if ( _curr_index > len ) 149 | _curr_index = len; 150 | } 151 | else if ( k == SDLK_RETURN ) 152 | { 153 | onblur(); // this shouldn't be here, or is it okey? Should menu handle it? 154 | submit(); 155 | return; 156 | } 157 | else if ( _curr_index < 39 && key != 0 ) 158 | { 159 | // Insert the char into 160 | uni_place(_str, key, 40, _curr_index); 161 | _curr_index++; 162 | } 163 | 164 | renderText(); 165 | } 166 | 167 | void InputNode::str(const char* s) 168 | { 169 | int i = 0; 170 | while ( *s != '\0' && i < 40 ) 171 | { 172 | _str[i++] = *s; 173 | s++; 174 | } 175 | _str[i] = '\0'; 176 | 177 | renderText(); 178 | } 179 | 180 | void InputNode::empty() 181 | { 182 | _curr_index = 0; 183 | str(""); 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /game.h: -------------------------------------------------------------------------------- 1 | /* 2 | * game.h 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-18. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #ifndef __GAME_H__ 29 | #define __GAME_H__ 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include "SDL.h" 36 | #include "SDL/SDL_ttf.h" 37 | 38 | const int GAME_FPS = 50; 39 | const int PAUSE_FPS = 50; 40 | const int ADJACENT_BALLS = 3; 41 | const int GAME_LIVES = 6; 42 | const int GAME_RECORD_FRAMES = GAME_FPS * 10; // 10 seconds 43 | 44 | class Grid; 45 | class Arrow; 46 | class Sprite; 47 | class Background; 48 | class Points; 49 | class Menu; 50 | class Music; 51 | 52 | typedef std::list rect_list; 53 | 54 | class Game 55 | { 56 | private: 57 | typedef std::list list; 58 | typedef list queue; 59 | 60 | SDL_Surface *_screen; 61 | SDL_Surface *_buffer; 62 | SDL_Rect *_size; 63 | SDL_Event _event; 64 | 65 | TTF_Font *_font; 66 | 67 | int _width; 68 | int _height; 69 | 70 | list _sprites; 71 | rect_list _dirty_rects; 72 | 73 | int _fps; 74 | Uint32 _last_ticks; 75 | double _tdelta; 76 | 77 | // Queues are used so they don't interfere when looping through the sprite_list 78 | queue _rem_queue; 79 | queue _add_queue; 80 | 81 | // This flag will be set to true if anything has been removed from the sprites list 82 | bool _is_dirty; 83 | 84 | // This flag indicates whether Game can use keyevents (such as the user pressing p for pause) or not 85 | bool _hold_key_events; 86 | 87 | // This flag indicates whether the everything needs to be redrawn 88 | bool _redraw; 89 | 90 | Grid *_grid; 91 | Arrow *_arrow; 92 | Background *_background; 93 | Points *_points_sprite; 94 | Menu *_menu; 95 | Music *_music; 96 | 97 | bool _running; 98 | 99 | // Gameplay-related 100 | short _lives[3]; 101 | long _points, _last_points; 102 | int _points_quantifier; 103 | 104 | bool _is_paused; 105 | 106 | int _volume; 107 | 108 | // Recording (debug) 109 | bool _is_recording, _is_playing; 110 | int _current_frame, _play_frame, _rec_slow; 111 | SDL_Surface *_rec_frames[GAME_RECORD_FRAMES]; 112 | SDL_Surface *_playing, *_recording; 113 | void rec(); 114 | void recStart(); 115 | void recEnd(); 116 | void recPlay(); 117 | void recStop(); 118 | void recFrame(); 119 | 120 | void setVolume(); 121 | public: 122 | Game(); 123 | Game(SDL_Surface* screen); 124 | ~Game(); 125 | 126 | // Play-related 127 | void start(); 128 | void togglePause(); 129 | void pause(); 130 | bool isPaused() const { return _is_paused; }; 131 | 132 | void holdKeyEvents(bool s) { _hold_key_events = s; }; 133 | 134 | // Getters 135 | SDL_Surface *buffer() { return _buffer; }; 136 | SDL_Rect *size() { return _size; }; 137 | double tdelta() { return _tdelta; }; 138 | short lives() { return _lives[0]; }; 139 | long points() const { return _points; }; 140 | long lastPoints() const { return _last_points; }; 141 | 142 | // Common sprites 143 | Grid *grid() const { return _grid; }; 144 | Arrow *arrow() const { return _arrow; }; 145 | Background *background() const { return _background; }; 146 | Music *music() const { return _music; }; 147 | 148 | // Sprites 149 | void addSprite(Sprite*); 150 | void removeSprite(Sprite*); 151 | void cleanupList(); 152 | 153 | // Dirty rect animation 154 | void markForRedraw() { _redraw = true; }; 155 | 156 | void cap(); 157 | 158 | // Screen 159 | void setWidth(int width) { _width = width; }; 160 | void setHeight(int height) { _height = height; }; 161 | 162 | // Game 163 | void loop(); 164 | void handleEvents(); 165 | void draw(); 166 | void tick(); 167 | 168 | // Sound 169 | void volumeUp(); 170 | void volumeDown(); 171 | bool toggleMusic(); 172 | bool isPlayingMusic(); 173 | 174 | // Lives 175 | void checkRowCount(); 176 | void decrementLives(); 177 | void lost(); 178 | void win(); 179 | void showSubmit(); 180 | void reset(); 181 | 182 | // Points 183 | void startPointsAdding() { _points_quantifier = 0; }; 184 | void addPointsNormal(); 185 | void addPointsJumbo(); 186 | 187 | // DEBUGGING 188 | void printSelf() 189 | { 190 | std::cout << "\n=== Current state of affairs: ===\n"; 191 | std::cout << "Screen size: " << _width << "x" << _height << '\n'; 192 | std::cout << "Fps is: " << _fps << '\n'; 193 | std::cout << "sprite_list size is at: " << _sprites.size() << '\n'; 194 | std::cout << "Dirty rects is at: " << _dirty_rects.size() << '\n'; 195 | std::cout << "rem_queue is size is " << _rem_queue.size() << " and add_queue is " << _add_queue.size() << '\n'; 196 | std::cout << "lives is " << _lives[0] << " " << _lives[1] << " " << _lives[2] << '\n'; 197 | std::cout << "You have " << _points << " and last_points is " << _last_points << '\n'; 198 | std::cout << "points quantifier is " << _points_quantifier << '\n'; 199 | std::cout << "... and is_paused is " << _is_paused << " and the game volum is " << _volume << std::endl; 200 | } 201 | }; 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /scorescrollnode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * scorescrollnode.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-26. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "SDL.h" 33 | #include "SDL/SDL_ttf.h" 34 | 35 | #include "screen.h" 36 | #include "mysdl.h" 37 | 38 | #include "menu.h" 39 | #include "scorescrollnode.h" 40 | 41 | ScoreScrollNode::ScoreScrollNode(Screen *screen): Node(screen) 42 | { 43 | _image = NULL; 44 | 45 | _pos.x = 175; 46 | _pos.y = 30; 47 | _pos.w = 160; 48 | _pos.h = 135; 49 | 50 | _name_color.r = _name_color.g = _name_color.b = 0xFF; 51 | _score_color.r = 0xF1; 52 | _score_color.g = 0x72; 53 | _score_color.b = 0xB7; 54 | 55 | _font = load_font(16); 56 | 57 | _scroll_rect.x = 0; 58 | _scroll_rect.y = 0; 59 | _scroll_rect.w = _pos.w; 60 | _scroll_rect.h = _pos.h; 61 | 62 | _bg_color.r = _bg_color.b = 0; 63 | _bg_color.g = 0; 64 | 65 | _font_height = TTF_FontHeight(_font); 66 | } 67 | 68 | void ScoreScrollNode::draw(SDL_Surface *surface) 69 | { 70 | if ( _scores.size() == 0 ) 71 | return; 72 | SDL_Rect rect = _screen->makeRect(_pos); 73 | SDL_BlitSurface(_image, NULL, surface, &rect); 74 | } 75 | 76 | // This should be rewritten 77 | void ScoreScrollNode::renderText() 78 | { 79 | // Init variables 80 | SDL_Surface *name, *points, *place, *surface; 81 | surface = _screen->game()->buffer(); 82 | 83 | Highscore *hs; 84 | std::stringstream str, str_place; 85 | 86 | // The texts position on text 87 | SDL_Rect position; 88 | position.x = 0; 89 | 90 | // Get scores 91 | highscore_list &scores = _screen->menu()->manager.get(); 92 | 93 | // Create surface on which to draw stuff 94 | int num_scores = scores.size(); 95 | int height = num_scores * 2 * _font_height; 96 | 97 | _height = height; 98 | 99 | // Kill old stuff 100 | for ( score_triplet_list::iterator iter = _scores.begin(); iter != _scores.end(); iter++ ) 101 | { 102 | SDL_Surface **surfaces = (*iter); 103 | SDL_FreeSurface(surfaces[0]); 104 | SDL_FreeSurface(surfaces[1]); 105 | SDL_FreeSurface(surfaces[2]); 106 | delete [] surfaces; 107 | } 108 | 109 | _scores.erase(_scores.begin(), _scores.end()); 110 | 111 | int i = 0; 112 | for ( highscore_list::iterator iter = scores.begin(); iter != scores.end(); iter++, i++ ) 113 | { 114 | hs = *iter; 115 | str << hs->score << " points"; 116 | str_place << (i + 1) << ". "; 117 | 118 | // Render texts 119 | place = TTF_RenderText_Blended(_font, str_place.str().c_str(), _name_color); 120 | name = TTF_RenderUNICODE_Blended(_font, hs->name, _name_color); 121 | points = TTF_RenderText_Blended(_font, str.str().c_str(), _score_color); 122 | 123 | SDL_Surface **surfaces = new SDL_Surface*[3]; 124 | surfaces[0] = place; 125 | surfaces[1] = name; 126 | surfaces[2] = points; 127 | _scores.push_back(surfaces); 128 | 129 | str.str(""); 130 | str_place.str(""); 131 | } 132 | 133 | // draw() 134 | // the area on which we draw ourselves 135 | SDL_Rect rect = _screen->makeRect(_pos); 136 | 137 | SDL_FreeSurface(_image); 138 | _image = SDL_CreateRGBSurface(surface->flags, _pos.w, _pos.h, surface->format->BitsPerPixel, 0, 0, 0, 0); 139 | SDL_BlitSurface(_screen->menu()->overlay(), &rect, _image, NULL); 140 | 141 | // unit height is the height of one score element 142 | int unit_height = _font_height * 2; 143 | 144 | int start_score = _scroll_rect.y / unit_height; 145 | 146 | // Check bounds for end_score 147 | int end_score = _pos.h / unit_height + 2 + start_score; 148 | end_score = (end_score > _scores.size()) ? _scores.size() : end_score; 149 | 150 | SDL_Rect cp_position; 151 | position.x = 0; 152 | position.y = start_score * unit_height - _scroll_rect.y; 153 | 154 | score_triplet_list::iterator iter, to; 155 | iter = _scores.begin() + start_score; 156 | to = _scores.begin() + end_score; 157 | 158 | for (; iter != to; iter++) 159 | { 160 | SDL_Surface **surfaces = (*iter); 161 | 162 | // Place 163 | cp_position = position; 164 | SDL_BlitSurface(surfaces[0], NULL, _image, &cp_position); 165 | 166 | // Name 167 | position.x += surfaces[0]->clip_rect.w; 168 | cp_position = position; 169 | SDL_BlitSurface(surfaces[1], NULL, _image, &cp_position); 170 | 171 | // Points 172 | position.x -= surfaces[0]->clip_rect.w; 173 | position.y += _font_height; 174 | cp_position = position; 175 | SDL_BlitSurface(surfaces[2], NULL, _image, &cp_position); 176 | 177 | position.y += _font_height; 178 | } 179 | } 180 | 181 | void ScoreScrollNode::onshow() 182 | { 183 | renderText(); 184 | } 185 | 186 | void ScoreScrollNode::ondrag(int relx, int rely) 187 | { 188 | _scroll_rect.y += -rely; 189 | 190 | if ( _scroll_rect.y > _height - _pos.h ) 191 | _scroll_rect.y = _height - _pos.h; 192 | 193 | if ( _scroll_rect.y < 0 ) 194 | _scroll_rect.y = 0; 195 | 196 | renderText(); 197 | } 198 | 199 | void ScoreScrollNode::onscroll(bool up) 200 | { 201 | ondrag(0, (up ? 10 : -10)); 202 | } 203 | -------------------------------------------------------------------------------- /arrow.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * arrow.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-22. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "SDL_rotozoom.h" 31 | #include "SDL_image.h" 32 | 33 | #include "arrow.h" 34 | #include "game.h" 35 | #include "music.h" 36 | #include "ball.h" 37 | #include "vector.h" 38 | 39 | Arrow::Arrow(Game *game): Sprite(game) 40 | { 41 | _origin = IMG_Load("arrow.png"); 42 | if ( ! _origin ) 43 | { 44 | std::cerr << "Could not load arrow.png: " << IMG_GetError() << std::endl; 45 | } 46 | // Create copy of _origin 47 | _image = SDL_DisplayFormatAlpha(_origin); 48 | 49 | _pos = _origin->clip_rect; 50 | _pos.x = _game->size()->w / 2 - _pos.w / 2; 51 | _pos.y = _game->size()->h - BALL_HEIGHT / 2; 52 | 53 | // Queue balls 54 | // Create one more than specified, for the current ball 55 | for ( int i = 0; i <= ARROW_QUEUE_SIZE; i++ ) 56 | _queue.push_back(Ball::create(_game)); 57 | 58 | setCurrent(); 59 | //prepareQueue(); 60 | 61 | //std::cout << "Current color is " << Ball::getC(_current_ball->color()) << std::endl; 62 | } 63 | 64 | Arrow::~Arrow() 65 | { 66 | SDL_FreeSurface(_origin); 67 | SDL_FreeSurface(_image); 68 | } 69 | 70 | void Arrow::prepareQueue() 71 | { 72 | // Queue balls 73 | // Create one more than specified, for the current ball 74 | for ( int i = 0; i <= ARROW_QUEUE_SIZE; i++ ) 75 | _queue.push_back(Ball::create(_game)); 76 | 77 | setCurrent(); 78 | } 79 | 80 | void Arrow::tick() 81 | { 82 | if ( _game->isPaused() ) 83 | return; 84 | 85 | int i = 0; 86 | for ( ball_queue::iterator iter = _queue.begin(); iter != _queue.end(); iter++, i++ ) 87 | (*iter)->visible( !(i >= _game->lives()) ); 88 | } 89 | 90 | void Arrow::draw() 91 | { 92 | if ( _game->isPaused() ) 93 | return; 94 | 95 | SDL_Rect pos = _rect; // Make copy for SDL clipping goodness 96 | SDL_BlitSurface(_image, NULL, _game->buffer(), &pos); 97 | } 98 | 99 | void Arrow::handleEvent(const SDL_Event &event) 100 | { 101 | if ( _game->isPaused() ) 102 | return; 103 | 104 | switch (event.type) { 105 | case SDL_MOUSEBUTTONDOWN: 106 | if ( ! _game->music()->collidesWith(event.button.x, event.button.y) ) 107 | release(); 108 | break; 109 | 110 | case SDL_MOUSEMOTION: 111 | _mouse_pos.x(event.motion.x); 112 | _mouse_pos.y(event.motion.y); 113 | 114 | rotateToMouse(); 115 | break; 116 | } 117 | } 118 | 119 | void Arrow::rotateToMouse() 120 | { 121 | //// 122 | // Position and rotate arrow 123 | Vector v(_pos.x, _pos.y); 124 | 125 | // Add 90 to the angle so we are working in line with the bottom axis 126 | double ang = v.angleSeparating(_mouse_pos) + 90; 127 | 128 | // Ensure angle is not out of bounds. What, no max/min-function? 129 | ang = (ang < MIN_ANGLE) ? MIN_ANGLE : ((ang > MAX_ANGLE) ? MAX_ANGLE : ang); 130 | 131 | _current_ball->_vel.angle(-ang); 132 | 133 | // Remove the 90 again to rotate correctly 134 | _angle = ang - 90; 135 | 136 | SDL_FreeSurface(_image); 137 | _image = rotozoomSurface(_origin, _angle, 1, 1); 138 | 139 | // Adjust position to center 140 | _rect = _image->clip_rect; 141 | _rect.x = _pos.x - _image->clip_rect.w / 2; 142 | _rect.y = _pos.y - _image->clip_rect.h / 2; 143 | 144 | //// 145 | // Position balls 146 | Vector ballpos(0, 40); 147 | ballpos.angle(-(_angle + 90)); 148 | 149 | _current_ball->xPos(_game->size()->w / 2 - BALL_WIDTH / 2 - _pos.w / 2 + ballpos.x()); 150 | _current_ball->yPos(_game->size()->h - BALL_HEIGHT + ballpos.y()); 151 | 152 | // Queue balls 153 | int i = 0; 154 | for ( ball_queue::iterator iter = _queue.begin(); iter != _queue.end(); iter++, i++ ) 155 | { 156 | Ball *ball = (*iter); 157 | int w = ball->size()->w; 158 | ball->xPos((w * ARROW_QUEUE_SIZE + 10) - i * w); 159 | ball->yPos(_game->size()->h - w - 10); 160 | } 161 | } 162 | 163 | void Arrow::setCurrent(bool is_in_game) 164 | { 165 | // Set new current ball by setting the front 166 | // of the queue to _current_ball and then 167 | // removing it from the queue. This means that 168 | // The active ball is flushed into space 169 | _current_ball = _queue.front(); 170 | _current_ball->setState(Ball::Queued); 171 | _current_ball->_vel.angle(-1 * (_angle + 90)); 172 | _current_ball->active(); 173 | 174 | _queue.remove(_current_ball); 175 | 176 | // Add to end 177 | Ball* b = Ball::create(_game); 178 | if ( is_in_game ) 179 | b->setRemainingColor(); 180 | _queue.push_back(b); 181 | 182 | // Rotate current ball to mouse position angle 183 | rotateToMouse(); 184 | } 185 | 186 | void Arrow::checkQueueColors() 187 | { 188 | // Only the ones not visible should be checked. 189 | // Because the not visible ones are not technically part 190 | // of the queue. Maybe the queue should be of variable size? 191 | for ( ball_queue::iterator iter = _queue.begin(); iter != _queue.end(); iter++ ) 192 | if ( ! (*iter)->visible() ) 193 | (*iter)->ensureColorExists(); 194 | } 195 | 196 | void Arrow::release() 197 | { 198 | if ( ! isReady() ) 199 | return; 200 | 201 | _current_ball->setState(Ball::Moving); 202 | setReady(false); 203 | 204 | setCurrent(true); 205 | } 206 | -------------------------------------------------------------------------------- /ball.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ball.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-07. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | #include "SDL.h" 32 | #include "SDL_rotozoom.h" 33 | 34 | #include "mysdl.h" 35 | #include "ballmanager.h" 36 | #include "sound.h" 37 | 38 | #include "game.h" 39 | #include "background.h" 40 | #include "grid.h" 41 | #include "fx.h" 42 | #include "arrow.h" 43 | 44 | #include "ball.h" 45 | 46 | Ball* Ball::create(Game *game) 47 | { 48 | Ball *b = new Ball(game); 49 | game->addSprite(b); 50 | return b; 51 | } 52 | 53 | Ball::Ball(Game *game): Sprite(game), _was_dangly(false), _state(Queued) 54 | { 55 | setColor(Random); 56 | 57 | _rect = _image->clip_rect; 58 | 59 | _vel.x(0); 60 | _vel.y(_game->size()->h * 2); 61 | 62 | _grid_x = _grid_y = -1; 63 | 64 | _anim = Fx(); 65 | _anim.mode(Fx::Single); 66 | } 67 | 68 | Ball::~Ball() 69 | { 70 | // The ball can be destructed whilst animating, 71 | // that is why we have to free the surface 72 | // created by rotozoomSurface here. 73 | if ( _image != _sprite ) 74 | { 75 | SDL_FreeSurface(_image); 76 | } 77 | } 78 | 79 | void Ball::setColor(Colors color) 80 | { 81 | color = (color == Random) ? BallManager::randomColor() : color; 82 | _image = _sprite = BallManager::load(color); 83 | _color = color; 84 | } 85 | 86 | void Ball::setRemainingColor() 87 | { 88 | setColor(BallManager::randomRemainingColor(_game->grid())); 89 | } 90 | 91 | void Ball::ensureColorExists() 92 | { 93 | if ( ! BallManager::colorExists(_color) ) 94 | { 95 | setRemainingColor(); 96 | active(); 97 | } 98 | } 99 | 100 | void Ball::draw() 101 | { 102 | if ( _game->isPaused() || ! visible() ) 103 | return; 104 | 105 | _rect.x = _pos.x(); 106 | _rect.y = _pos.y(); 107 | 108 | // Make copy because SDL clips the passed rect (or so I think) 109 | SDL_Rect cp_rect = _rect; 110 | 111 | // Center it to its position 112 | int half = BALL_WIDTH / 2 - _image->clip_rect.w / 2; 113 | cp_rect.x += half; 114 | cp_rect.y += half; 115 | 116 | SDL_BlitSurface(_image, NULL, _game->buffer(), &cp_rect); 117 | } 118 | 119 | void Ball::tick() 120 | { 121 | if ( _game->isPaused() ) 122 | return; 123 | 124 | if ( _state == Moving ) 125 | { 126 | Vector orig_pos(_pos); 127 | 128 | Ball *hit; 129 | // Break movement into smaller pieces and analyze them 130 | int pieces = 5, i; 131 | for ( i = 1; i <= pieces * 2; i++ ) 132 | { 133 | _pos = orig_pos + (_vel * (_game->tdelta() / pieces * i)); 134 | hit = _game->grid()->inCollision(*this); 135 | if ( hit ) break; 136 | } 137 | 138 | if ( i > pieces ) 139 | _pos = orig_pos + _vel * _game->tdelta(); 140 | 141 | xPos(_pos.x()); 142 | yPos(_pos.y()); 143 | 144 | if ( _pos.y() < 0 || (hit = _game->grid()->inCollision(*this)) ) 145 | { 146 | _pos = orig_pos + _vel * (_game->tdelta() / pieces * (i - 1)); 147 | 148 | // Okey, so I've hit something. 149 | // What do I do next? 150 | // 1. pin myself. (check) 151 | // 2. settle on a grid position with my homies 152 | // 3. add myself to Grid (check) 153 | 154 | setState(Pinned); 155 | 156 | // Remove myself from the empty movement void 157 | _game->arrow()->setReady(true); 158 | 159 | // Animate bouncy bounce 160 | active(); 161 | satisfyGrid(); 162 | 163 | // Add myself to the warm land of Grid 164 | _game->grid()->addBall(this); 165 | 166 | // I've hit a wall 167 | if ( ! hit ) 168 | { 169 | _game->decrementLives(); 170 | } 171 | 172 | play_ball_thud(); 173 | 174 | return; 175 | } 176 | 177 | const SDL_Rect *size = _game->size(); 178 | 179 | // Collision detection, wee! 180 | if ( _pos.x() < 0 ) 181 | { 182 | _vel._x *= -1; 183 | xPos(0); 184 | 185 | play_ball_bounce(); 186 | } 187 | else if ( _pos.x() + _rect.w > size->w ) 188 | { 189 | _vel._x *= -1; 190 | xPos(size->w - _rect.w); 191 | 192 | play_ball_bounce(); 193 | } 194 | 195 | if ( _pos.y() + _rect.h > size->h ) 196 | { 197 | _vel._y *= -1; 198 | yPos(size->h - _rect.h); 199 | 200 | play_ball_bounce(); 201 | } 202 | // The end! 203 | } 204 | 205 | if ( _anim.isRunning() ) 206 | { 207 | SDL_FreeSurface(_image); 208 | setImage(rotozoomSurface(_sprite, 1, _anim.step(), 1)); 209 | 210 | // Has the motion stopped 211 | if ( ! _anim.isRunning() ) 212 | { 213 | SDL_FreeSurface(_image); 214 | _image = _sprite; 215 | } 216 | } 217 | } 218 | 219 | Vector Ball::calculateGrid() 220 | { 221 | int grid_size = _game->size()->w / BALL_GRID_W; 222 | 223 | int center_x = _pos.y() + _rect.h / 2; 224 | int center_y = _pos.x() + _rect.w / 2; 225 | 226 | // These calculated are the idealised (where I really am) x/y-grid positions 227 | int y = center_x / grid_size; 228 | int x = (center_y - (y % 2 ? 12 : 0)) / grid_size; 229 | 230 | return Vector(x, y); 231 | } 232 | 233 | void Ball::satisfyGrid() 234 | { 235 | Vector grid = calculateGrid(); 236 | int x = grid.x(), y = grid.y(); 237 | 238 | x = (x >= BALL_GRID_W) ? BALL_GRID_W - 1 : x; 239 | 240 | if ( _game->grid()->hasBallOn(x, y) ) 241 | { 242 | y++; 243 | } 244 | 245 | _grid_x = x; 246 | _grid_y = y; 247 | 248 | gridToPos(); 249 | } 250 | 251 | void Ball::gridToPos() 252 | { 253 | int dd = _game->size()->w / BALL_GRID_W; 254 | 255 | int pos_x = dd * _grid_x + (_grid_y % 2 ? 12 : 0); 256 | int pos_y = dd * _grid_y; 257 | 258 | xPos(pos_x); 259 | yPos(pos_y); 260 | } 261 | 262 | bool Ball::collidesWith(Ball &ball) 263 | { 264 | return _pos.distanceBetween(ball.pos()) < BALL_WIDTH - 6; 265 | } 266 | 267 | void Ball::initFx() 268 | { 269 | if ( _anim.isRunning() ) 270 | return; 271 | 272 | // Make copy of the ball that we can enlarge 273 | _image = SDL_DisplayFormatAlpha(_sprite); 274 | } 275 | 276 | void Ball::active() 277 | { 278 | if ( _anim.isRunning() ) 279 | return; 280 | 281 | initFx(); 282 | 283 | // Prepare animation 284 | _anim.value(1); 285 | _anim.duration(250); 286 | _anim.transition(active_transition); 287 | 288 | // Active animation 289 | _anim.start(); 290 | } 291 | 292 | void Ball::bam() 293 | { 294 | initFx(); 295 | 296 | _anim.stop(); 297 | 298 | _anim.transition(bam_transition); 299 | _anim.value(1); 300 | _anim.duration(40); 301 | _anim.start(); 302 | 303 | play_ball_die(); 304 | } 305 | 306 | double active_transition(double x) 307 | { 308 | return 2 * (x - 0) * (x - 1) + 1; 309 | }; 310 | 311 | double bam_transition(double x) 312 | { 313 | return 1 + x; 314 | }; 315 | -------------------------------------------------------------------------------- /grid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * grid.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-12-11. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | 30 | #include "ballmanager.h" 31 | 32 | #include "arrow.h" 33 | #include "game.h" 34 | #include "grid.h" 35 | 36 | Grid::Grid(Game *game): Sprite(game), _remove_top(NULL) {} 37 | 38 | void Grid::tick() 39 | { 40 | if ( ! _remove_top ) 41 | { 42 | _remove_top = _remove_list.front(); 43 | if ( _remove_top ) 44 | { 45 | _remove_top->bam(); 46 | _remove_list.remove(_remove_top); 47 | } 48 | else 49 | { 50 | _game->startPointsAdding(); 51 | } 52 | } 53 | 54 | if ( _remove_top ) 55 | { 56 | _remove_top->tick(); 57 | if ( ! _remove_top->_anim.isRunning() ) 58 | { 59 | if ( _remove_top->wasDangly() ) 60 | _game->addPointsJumbo(); 61 | else 62 | _game->addPointsNormal(); 63 | 64 | removeBall(_remove_top); 65 | _remove_top = NULL; 66 | } 67 | } 68 | } 69 | 70 | void Grid::draw() {} 71 | 72 | void Grid::removeBall(Ball *ball) 73 | { 74 | _game->removeSprite(ball); 75 | _game->arrow()->checkQueueColors(); 76 | 77 | if ( _balls.size() == 0 ) 78 | { 79 | _game->win(); 80 | } 81 | } 82 | 83 | void Grid::addBall(Ball *ball) 84 | { 85 | _balls.push_back(ball); 86 | 87 | locateGroups(*ball); 88 | handleDanglies(); 89 | 90 | _game->checkRowCount(); 91 | } 92 | 93 | void Grid::removeBalls(ball_list &balls) 94 | { 95 | for ( ball_list::iterator iter = balls.begin(); iter != balls.end(); iter++ ) 96 | { 97 | _balls.remove(*iter); 98 | _remove_list.push_back(*iter); 99 | } 100 | 101 | // We have to make sure that every color 102 | // in the queue actually exists on the field 103 | BallManager::prepRemList(this); 104 | } 105 | 106 | void Grid::generateRow(int rows = 1, bool is_startrow = false) 107 | { 108 | // Move old balls up a Y-coordinate 109 | for ( ball_list::iterator iter = _balls.begin(); iter != _balls.end(); iter++ ) 110 | (**iter).gridY((**iter).gridY() + rows); 111 | 112 | for ( int y = 0; y < rows; y++ ) 113 | for ( int x = 0; x < BALL_GRID_W; x++ ) 114 | { 115 | Ball *ball = Ball::create(_game); 116 | if ( ! is_startrow ) 117 | ball->setRemainingColor(); 118 | ball->setState(Ball::Pinned); 119 | ball->gridX(x); 120 | ball->gridY(y); 121 | 122 | _balls.push_back(ball); 123 | } 124 | } 125 | 126 | void Grid::generateStartRows() 127 | { 128 | generateRow(BALL_GRID_H / 2, true); 129 | } 130 | 131 | int Grid::countRows() 132 | { 133 | int rows = 0; 134 | for ( ball_list::iterator iter = _balls.begin(); iter != _balls.end(); iter++ ) 135 | rows = (**iter).gridY() > rows ? (**iter).gridY() : rows; 136 | return rows; 137 | } 138 | 139 | void Grid::emptyRows() 140 | { 141 | // Delete balls 142 | for ( ball_list::iterator ball = _balls.begin(); ball != _balls.end(); ball++ ) 143 | removeBall(*ball); 144 | 145 | _balls.erase(_balls.begin(), _balls.end()); 146 | } 147 | 148 | Ball* Grid::inCollision(Ball &ball) 149 | { 150 | for ( ball_list::iterator iter = _balls.begin(); iter != _balls.end(); iter++ ) 151 | { 152 | if ( ball.collidesWith((**iter)) ) 153 | return *iter; 154 | } 155 | return NULL; 156 | } 157 | 158 | bool Grid::hasBallOn(int x, int y) 159 | { 160 | return getBallOn(x, y) != NULL; 161 | } 162 | 163 | Ball *Grid::getBallOn(int x, int y) 164 | { 165 | for ( ball_list::iterator iter = _balls.begin(); iter != _balls.end(); iter++ ) 166 | { 167 | if ( (**iter).gridX() == x && (**iter).gridY() == y ) 168 | return *iter; 169 | } 170 | return NULL; 171 | } 172 | 173 | Ball *Grid::getBallWithColor(int x, int y, Ball::Colors color) 174 | { 175 | Ball *match = getBallOn(x, y); 176 | if ( match == NULL || match->_color != color ) 177 | return NULL; 178 | return match; 179 | } 180 | 181 | void Grid::locateGroups(Ball &relativeTo) 182 | { 183 | ball_list found; 184 | search(relativeTo, found); 185 | 186 | if ( found.size() >= ADJACENT_BALLS ) 187 | removeBalls(found); 188 | else 189 | _game->decrementLives(); 190 | } 191 | 192 | void Grid::search(Ball &ball, ball_list &result) 193 | { 194 | grid_list searched; 195 | doSearch(ball, result, searched, true, true); 196 | 197 | // Delete everything in searched list 198 | for ( grid_list::iterator iter = searched.begin(); iter != searched.end(); iter++ ) 199 | delete *iter; 200 | } 201 | 202 | void Grid::handleDanglies() 203 | { 204 | grid_list searched; 205 | 206 | typedef std::list island_list; 207 | island_list islands; 208 | 209 | for ( ball_list::iterator iter = _balls.begin(); iter != _balls.end(); iter++ ) 210 | { 211 | ball_list *result = new ball_list; 212 | if ( doSearch((**iter), *result, searched, true, false) ) 213 | islands.push_back(result); 214 | else 215 | delete result; 216 | } 217 | 218 | for ( island_list::iterator iter = islands.begin(); iter != islands.end(); iter++ ) 219 | { 220 | bool is_connected = false; 221 | ball_list *balls = (*iter); 222 | 223 | for ( ball_list::iterator i = balls->begin(); i != balls->end(); i++ ) 224 | if ( (**i).gridY() == 0 ) 225 | is_connected = true; 226 | 227 | if ( ! is_connected ) 228 | { 229 | for ( ball_list::iterator iter = balls->begin(); iter != balls->end(); iter++ ) 230 | (**iter).wasDangly(true); 231 | removeBalls(*balls); 232 | } 233 | 234 | delete balls; 235 | } 236 | 237 | // Delete everything in searched list 238 | for ( grid_list::iterator iter = searched.begin(); iter != searched.end(); iter++ ) 239 | delete *iter; 240 | } 241 | 242 | // Will return true if anything was searched at all. 243 | // the elements in searched has to be deleted after using 244 | bool Grid::doSearch(Ball &ball, ball_list &result, grid_list &searched, bool recursive, bool use_color) 245 | { 246 | /* 247 | * Search: 248 | * (x+1, y-1): 1 249 | * (x, y-1): 2 250 | * (x-1, y): 3 251 | * (x+1, y): 4 252 | * (x+1, y+1): 5 253 | * (x, y+1): 6 254 | */ 255 | 256 | int x = ball.gridX(), y = ball.gridY(); 257 | Ball::Colors c = ball._color; 258 | 259 | int even_x = ((y % 2) ? 1 : -1); 260 | 261 | int paths[7][2] = { 262 | { even_x, -1}, // 1 263 | { 0, -1}, // 2 264 | {-1, 0}, // 3 265 | { 1, 0}, // 4 266 | { even_x, 1}, // 5 267 | { 0, 1}, // 6, 268 | { 0, 0} // Me 269 | }; 270 | 271 | bool did_search = false; 272 | for ( int i = 0; i < 7; i++ ) 273 | { 274 | int px = paths[i][0] + x, 275 | py = paths[i][1] + y; 276 | 277 | // We don't need to search something we have already searched 278 | if ( gridListHas(searched, px, py) ) continue; 279 | 280 | did_search = true; 281 | 282 | GridPos *grid_pos = new GridPos(px, py); 283 | searched.push_back(grid_pos); 284 | 285 | Ball *match; 286 | if ( use_color ) 287 | match = getBallWithColor(px, py, c); 288 | else 289 | match = getBallOn(px, py); 290 | 291 | if ( match ) 292 | { 293 | result.push_back(match); 294 | if ( recursive ) 295 | doSearch(*match, result, searched, recursive, use_color); 296 | } 297 | } 298 | return did_search; 299 | } 300 | 301 | bool Grid::gridListHas(grid_list &list, int grid_x, int grid_y) 302 | { 303 | for ( grid_list::iterator iter = list.begin(); iter != list.end(); iter++ ) 304 | { 305 | if ( (**iter).x == grid_x && (**iter).y == grid_y ) 306 | return true; 307 | } 308 | return false; 309 | } 310 | -------------------------------------------------------------------------------- /game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * game.cpp 3 | * kattMickisShooter 4 | * 5 | * Created by Erik Andersson on 2009-11-18. 6 | * Copyright (c) 2010 Erik Andersson 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | #include "SDL_mixer.h" 32 | #include "SDL_image.h" 33 | 34 | #include "sound.h" 35 | #include "game.h" 36 | #include "sprite.h" 37 | 38 | #include "background.h" 39 | #include "arrow.h" 40 | #include "ball.h" 41 | #include "grid.h" 42 | #include "points.h" 43 | #include "menu.h" 44 | #include "music.h" 45 | 46 | const int START_VOLUME = MIX_MAX_VOLUME / 2 / 2; // 10th power of 2 47 | const int MAX_VOLUME = MIX_MAX_VOLUME; // 12th power of 2 48 | 49 | Game::Game() 50 | { 51 | std::cout << "Game() called" << std::endl; 52 | } 53 | 54 | Game::Game(SDL_Surface *screen): _running(true), _screen(screen), _points(0), _last_points(0), _points_quantifier(0) 55 | , _is_paused(false), _is_dirty(false), _volume(START_VOLUME), _is_recording(false), _is_playing(false) 56 | , _hold_key_events(false), _redraw(true), _rec_slow(0) 57 | { 58 | _size = &_screen->clip_rect; 59 | 60 | // Create buffer for drawing 61 | _buffer = SDL_DisplayFormatAlpha(_screen); 62 | 63 | // Init FPS capping 64 | _fps = GAME_FPS; 65 | _last_ticks = 0; 66 | 67 | // Create base sprites 68 | _arrow = new Arrow(this); 69 | _background = new Background(this); 70 | _grid = new Grid(this); 71 | _points_sprite = new Points(this); 72 | _menu = new Menu(this); 73 | _music = new Music(this); 74 | 75 | _sprites.push_back(_background); 76 | _sprites.push_back(_arrow); 77 | _sprites.push_back(_grid); 78 | _sprites.push_back(_points_sprite); 79 | _sprites.push_back(_menu); 80 | _sprites.push_back(_music); 81 | 82 | setVolume(); 83 | play_music(); 84 | 85 | // Begin game 86 | reset(); 87 | 88 | // Recording 89 | _recording = IMG_Load("recording.png"); 90 | _playing = IMG_Load("playing.png"); 91 | 92 | for ( int i = 0; i < GAME_RECORD_FRAMES; i++ ) 93 | _rec_frames[i] = NULL; 94 | } 95 | 96 | void Game::start() 97 | { 98 | _is_paused = false; 99 | _fps = GAME_FPS; 100 | } 101 | 102 | void Game::togglePause() 103 | { 104 | if ( _is_paused ) 105 | start(); 106 | else 107 | pause(); 108 | } 109 | 110 | void Game::pause() 111 | { 112 | _is_paused = true; 113 | _fps = PAUSE_FPS; 114 | } 115 | 116 | void Game::addSprite(Sprite *sprite) 117 | { 118 | _add_queue.push_back(sprite); 119 | } 120 | 121 | void Game::removeSprite(Sprite *sprite) 122 | { 123 | _is_dirty = true; 124 | _rem_queue.push_back(sprite); 125 | } 126 | 127 | void Game::loop() 128 | { 129 | while ( _running ) 130 | { 131 | if ( _redraw || _is_playing ) 132 | { 133 | draw(); 134 | } 135 | _redraw = false; 136 | 137 | tick(); 138 | cleanupList(); 139 | cap(); 140 | } 141 | } 142 | 143 | void Game::cap() 144 | { 145 | Uint32 curr_ticks = SDL_GetTicks(); 146 | int time_passed = curr_ticks - _last_ticks; 147 | int delay = 0; 148 | 149 | if ( _fps > 0 ) 150 | { 151 | delay = (1000 / _fps) - time_passed; 152 | if ( delay > 0 ) 153 | { 154 | SDL_Delay(delay); 155 | time_passed += delay; 156 | } 157 | } 158 | 159 | _last_ticks = curr_ticks; 160 | 161 | if ( delay > 0 ) 162 | time_passed -= delay; 163 | 164 | _tdelta = double(time_passed) / 1000.0; 165 | } 166 | 167 | void Game::cleanupList() 168 | { 169 | if ( ! _rem_queue.empty() ) 170 | _redraw = true; 171 | 172 | // Loop through rem queue and find things in add queue that have been removed already 173 | for ( queue::iterator iter = _rem_queue.begin(); iter != _rem_queue.end(); iter++ ) 174 | { 175 | for ( queue::iterator aiter = _add_queue.begin(); aiter != _add_queue.end(); aiter++ ) 176 | { 177 | if ( (*aiter)->ID == (*iter)->ID ) 178 | { 179 | _add_queue.erase(aiter); 180 | } 181 | } 182 | } 183 | 184 | // Remove elements from remove queue 185 | while ( ! _rem_queue.empty() ) 186 | { 187 | Sprite *d = _rem_queue.front(); 188 | _rem_queue.pop_front(); 189 | 190 | _sprites.remove(d); 191 | delete d; 192 | } 193 | 194 | // Add new elements 195 | while ( ! _add_queue.empty() ) 196 | { 197 | _sprites.push_back(_add_queue.front()); 198 | _add_queue.pop_front(); 199 | } 200 | 201 | _is_dirty = false; 202 | } 203 | 204 | void Game::handleEvents() 205 | { 206 | while ( SDL_PollEvent(&_event) ) 207 | { 208 | // Handle the sprites' events 209 | for ( list::iterator iter = _sprites.begin(); iter != _sprites.end(); iter++ ) 210 | { 211 | (*iter)->preTick(); 212 | (*iter)->handleEvent(_event); 213 | (*iter)->postTick(); 214 | 215 | if ( (*iter)->dirty() ) 216 | { 217 | _redraw = true; 218 | } 219 | } 220 | 221 | // Default events 222 | switch ( _event.type ) 223 | { 224 | case SDL_KEYDOWN: 225 | if ( _event.key.keysym.sym == SDLK_q ) 226 | { 227 | Uint8* keyState = SDL_GetKeyState(NULL); 228 | if ( keyState[SDLK_LMETA] || keyState[SDLK_RMETA] ) 229 | _running = false; 230 | } 231 | if ( _event.key.keysym.sym == SDLK_p && ! _hold_key_events ) 232 | { 233 | _menu->showScreen(Menu::Home); 234 | togglePause(); 235 | } 236 | else if ( _event.key.keysym.sym == SDLK_m && ! _hold_key_events ) 237 | { 238 | toggleMusic(); 239 | } 240 | else if ( _event.key.keysym.sym == SDLK_j && ! _hold_key_events ) 241 | { 242 | volumeDown(); 243 | } 244 | else if ( _event.key.keysym.sym == SDLK_k && ! _hold_key_events ) 245 | { 246 | volumeUp(); 247 | } 248 | else if ( _event.key.keysym.sym == SDLK_r && ! _hold_key_events ) 249 | recStart(); 250 | else if ( _event.key.keysym.sym == SDLK_t && ! _hold_key_events ) 251 | recPlay(); 252 | else if ( _event.key.keysym.sym == SDLK_a ) 253 | _arrow->current()->active(); 254 | 255 | break; 256 | case SDL_QUIT: 257 | _running = false; 258 | break; 259 | 260 | } 261 | } 262 | } 263 | 264 | void Game::draw() 265 | { 266 | if ( _is_playing ) 267 | recFrame(); 268 | else 269 | for ( list::iterator iter = _sprites.begin(); iter != _sprites.end(); iter++ ) 270 | (**iter).draw(); 271 | 272 | rec(); 273 | 274 | if ( (_is_recording || _is_playing) && _rec_slow++ % 10 == 0 ) 275 | { 276 | SDL_Rect place; 277 | place.x = _buffer->clip_rect.w - 70; 278 | place.y = _buffer->clip_rect.h - 70; 279 | if ( _is_recording ) 280 | SDL_BlitSurface(_recording, NULL, _buffer, &place); 281 | else 282 | SDL_BlitSurface(_playing, NULL, _buffer, &place); 283 | } 284 | 285 | SDL_BlitSurface(_buffer, &_buffer->clip_rect, _screen, &_screen->clip_rect); 286 | SDL_Flip(_screen); 287 | } 288 | 289 | void Game::tick() 290 | { 291 | handleEvents(); 292 | 293 | // Tick each sprite 294 | for ( list::iterator iter = _sprites.begin(); iter != _sprites.end(); iter++ ) 295 | { 296 | // The count-call is mostly a sanity check 297 | if ( ! _is_dirty || (_is_dirty && count(_rem_queue.begin(), _rem_queue.end(), *iter) == 0 ) ) 298 | { 299 | (*iter)->preTick(); 300 | (*iter)->tick(); 301 | (*iter)->postTick(); 302 | 303 | if ( (*iter)->dirty() ) 304 | { 305 | _redraw = true; 306 | } 307 | } 308 | } 309 | } 310 | 311 | void Game::setVolume() 312 | { 313 | Mix_Volume(-1, (float)_volume / (float)MAX_VOLUME * MIX_MAX_VOLUME); 314 | Mix_VolumeMusic((float)_volume / (float)MAX_VOLUME * MIX_MAX_VOLUME); 315 | } 316 | 317 | void Game::volumeUp() 318 | { 319 | _volume *= 2; 320 | _volume = _volume > MAX_VOLUME ? MAX_VOLUME : _volume; 321 | _volume = _volume == 0 ? 2 : _volume; 322 | 323 | setVolume(); 324 | } 325 | 326 | void Game::volumeDown() 327 | { 328 | _volume /= 2; 329 | setVolume(); 330 | } 331 | 332 | bool Game::toggleMusic() 333 | { 334 | if ( ! Mix_PausedMusic() ) { 335 | Mix_PauseMusic(); 336 | SDL_PauseAudio(1); 337 | return false; 338 | } 339 | SDL_PauseAudio(0); 340 | Mix_ResumeMusic(); 341 | return true; 342 | } 343 | 344 | bool Game::isPlayingMusic() 345 | { 346 | return ! Mix_PausedMusic(); 347 | } 348 | 349 | /* Gameplay-related */ 350 | 351 | void Game::checkRowCount() 352 | { 353 | if ( _grid->countRows() >= BALL_GRID_H ) 354 | { 355 | lost(); 356 | } 357 | } 358 | 359 | void Game::decrementLives() 360 | { 361 | _lives[0]--; 362 | if ( _lives[0] <= 0 ) 363 | { 364 | _grid->generateRow(1); 365 | 366 | if ( _lives[1] <= 1 ) 367 | { 368 | _lives[0] = _lives[1] = GAME_LIVES; 369 | } 370 | else 371 | { 372 | _lives[0] = --_lives[1]; 373 | } 374 | // checkRowCount(); 375 | } 376 | } 377 | 378 | void Game::lost() 379 | { 380 | play_lose(); 381 | 382 | showSubmit(); 383 | pause(); 384 | 385 | reset(); 386 | } 387 | 388 | void Game::win() 389 | { 390 | play_win(); 391 | 392 | _points *= 2; 393 | showSubmit(); 394 | pause(); 395 | 396 | reset(); 397 | } 398 | 399 | void Game::showSubmit() 400 | { 401 | _last_points = _points; 402 | _menu->showScreen(Menu::Submit); 403 | } 404 | 405 | void Game::reset() 406 | { 407 | _grid->emptyRows(); 408 | _grid->generateStartRows(); 409 | _lives[0] = _lives[1] = _lives[2] = GAME_LIVES; 410 | _points = 0; 411 | _arrow->setReady(true); 412 | } 413 | 414 | void Game::addPointsNormal() 415 | { 416 | _points += 10 * (int(_points_quantifier / 3) + 1); 417 | _points_quantifier += 1; 418 | } 419 | 420 | void Game::addPointsJumbo() 421 | { 422 | _points += 100; 423 | } 424 | 425 | void Game::rec() 426 | { 427 | if ( ! _is_recording ) 428 | return; 429 | 430 | _current_frame++; 431 | 432 | if ( _current_frame >= GAME_RECORD_FRAMES ) 433 | recEnd(); 434 | else 435 | { 436 | _rec_frames[_current_frame] = SDL_DisplayFormat(_buffer); 437 | } 438 | } 439 | 440 | void Game::recStart() 441 | { 442 | std::cout << "Start recording" << std::endl; 443 | 444 | // Clear old recordings 445 | for ( int i = 0; i < GAME_RECORD_FRAMES; i++ ) 446 | { 447 | SDL_FreeSurface(_rec_frames[i]); 448 | _rec_frames[i] = NULL; 449 | } 450 | 451 | _is_recording = true; 452 | _current_frame = 0; 453 | 454 | for ( int i = 0; i < GAME_RECORD_FRAMES; i++ ) 455 | _rec_frames[i] = NULL; 456 | } 457 | 458 | void Game::recEnd() 459 | { 460 | std::cout << "End recording" << std::endl; 461 | 462 | _is_recording = false; 463 | } 464 | 465 | void Game::recPlay() 466 | { 467 | std::cout << "Starting playback" << std::endl; 468 | 469 | _is_playing = true; 470 | _play_frame = 0; 471 | } 472 | 473 | void Game::recStop() 474 | { 475 | std::cout << "Ended playback\n"; 476 | 477 | _is_playing = false; 478 | } 479 | 480 | void Game::recFrame() 481 | { 482 | _play_frame++; 483 | 484 | if ( _play_frame >= GAME_RECORD_FRAMES ) 485 | recStop(); 486 | else 487 | { 488 | SDL_BlitSurface(_rec_frames[_play_frame], NULL, _buffer, NULL); 489 | } 490 | } 491 | 492 | Game::~Game() 493 | { 494 | for ( list::iterator iter = _sprites.begin(); iter != _sprites.end(); iter++ ) 495 | delete *iter; 496 | } 497 | -------------------------------------------------------------------------------- /SDLMain.m: -------------------------------------------------------------------------------- 1 | /* SDLMain.m - main entry point for our Cocoa-ized SDL app 2 | Initial Version: Darrell Walisser 3 | Non-NIB-Code & other changes: Max Horn 4 | 5 | Feel free to customize this file to suit your needs 6 | */ 7 | 8 | #include "SDL.h" 9 | #include "SDLMain.h" 10 | #include /* for MAXPATHLEN */ 11 | #include 12 | 13 | /* For some reaon, Apple removed setAppleMenu from the headers in 10.4, 14 | but the method still is there and works. To avoid warnings, we declare 15 | it ourselves here. */ 16 | @interface NSApplication(SDL_Missing_Methods) 17 | - (void)setAppleMenu:(NSMenu *)menu; 18 | @end 19 | 20 | /* Use this flag to determine whether we use SDLMain.nib or not */ 21 | #define SDL_USE_NIB_FILE 0 22 | 23 | /* Use this flag to determine whether we use CPS (docking) or not */ 24 | #define SDL_USE_CPS 1 25 | #ifdef SDL_USE_CPS 26 | /* Portions of CPS.h */ 27 | typedef struct CPSProcessSerNum 28 | { 29 | UInt32 lo; 30 | UInt32 hi; 31 | } CPSProcessSerNum; 32 | 33 | extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); 34 | extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); 35 | extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); 36 | 37 | #endif /* SDL_USE_CPS */ 38 | 39 | static int gArgc; 40 | static char **gArgv; 41 | static BOOL gFinderLaunch; 42 | static BOOL gCalledAppMainline = FALSE; 43 | 44 | static NSString *getApplicationName(void) 45 | { 46 | const NSDictionary *dict; 47 | NSString *appName = 0; 48 | 49 | /* Determine the application name */ 50 | dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); 51 | if (dict) 52 | appName = [dict objectForKey: @"CFBundleName"]; 53 | 54 | if (![appName length]) 55 | appName = [[NSProcessInfo processInfo] processName]; 56 | 57 | return appName; 58 | } 59 | 60 | #if SDL_USE_NIB_FILE 61 | /* A helper category for NSString */ 62 | @interface NSString (ReplaceSubString) 63 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; 64 | @end 65 | #endif 66 | 67 | @interface NSApplication (SDLApplication) 68 | @end 69 | 70 | @implementation NSApplication (SDLApplication) 71 | /* Invoked from the Quit menu item */ 72 | - (void)terminate:(id)sender 73 | { 74 | /* Post a SDL_QUIT event */ 75 | SDL_Event event; 76 | event.type = SDL_QUIT; 77 | SDL_PushEvent(&event); 78 | } 79 | @end 80 | 81 | /* The main class of the application, the application's delegate */ 82 | @implementation SDLMain 83 | 84 | /* Set the working directory to the .app's parent directory */ 85 | - (void) setupWorkingDirectory:(BOOL)shouldChdir 86 | { 87 | if (shouldChdir) 88 | { 89 | char parentdir[MAXPATHLEN]; 90 | CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle()); 91 | CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url); 92 | if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) { 93 | chdir(parentdir); /* chdir to the binary app's parent */ 94 | } 95 | CFRelease(url); 96 | CFRelease(url2); 97 | } 98 | } 99 | 100 | #if SDL_USE_NIB_FILE 101 | 102 | /* Fix menu to contain the real app name instead of "SDL App" */ 103 | - (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName 104 | { 105 | NSRange aRange; 106 | NSEnumerator *enumerator; 107 | NSMenuItem *menuItem; 108 | 109 | aRange = [[aMenu title] rangeOfString:@"SDL App"]; 110 | if (aRange.length != 0) 111 | [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]]; 112 | 113 | enumerator = [[aMenu itemArray] objectEnumerator]; 114 | while ((menuItem = [enumerator nextObject])) 115 | { 116 | aRange = [[menuItem title] rangeOfString:@"SDL App"]; 117 | if (aRange.length != 0) 118 | [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]]; 119 | if ([menuItem hasSubmenu]) 120 | [self fixMenu:[menuItem submenu] withAppName:appName]; 121 | } 122 | } 123 | 124 | #else 125 | 126 | static void setApplicationMenu(void) 127 | { 128 | /* warning: this code is very odd */ 129 | NSMenu *appleMenu; 130 | NSMenuItem *menuItem; 131 | NSString *title; 132 | NSString *appName; 133 | 134 | appName = getApplicationName(); 135 | appleMenu = [[NSMenu alloc] initWithTitle:@""]; 136 | 137 | /* Add menu items */ 138 | title = [@"About " stringByAppendingString:appName]; 139 | [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; 140 | 141 | [appleMenu addItem:[NSMenuItem separatorItem]]; 142 | 143 | title = [@"Hide " stringByAppendingString:appName]; 144 | [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; 145 | 146 | menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; 147 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; 148 | 149 | [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; 150 | 151 | [appleMenu addItem:[NSMenuItem separatorItem]]; 152 | 153 | title = [@"Quit " stringByAppendingString:appName]; 154 | [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; 155 | 156 | 157 | /* Put menu into the menubar */ 158 | menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; 159 | [menuItem setSubmenu:appleMenu]; 160 | [[NSApp mainMenu] addItem:menuItem]; 161 | 162 | /* Tell the application object that this is now the application menu */ 163 | [NSApp setAppleMenu:appleMenu]; 164 | 165 | /* Finally give up our references to the objects */ 166 | [appleMenu release]; 167 | [menuItem release]; 168 | } 169 | 170 | /* Create a window menu */ 171 | static void setupWindowMenu(void) 172 | { 173 | NSMenu *windowMenu; 174 | NSMenuItem *windowMenuItem; 175 | NSMenuItem *menuItem; 176 | 177 | windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; 178 | 179 | /* "Minimize" item */ 180 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; 181 | [windowMenu addItem:menuItem]; 182 | [menuItem release]; 183 | 184 | /* Put menu into the menubar */ 185 | windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; 186 | [windowMenuItem setSubmenu:windowMenu]; 187 | [[NSApp mainMenu] addItem:windowMenuItem]; 188 | 189 | /* Tell the application object that this is now the window menu */ 190 | [NSApp setWindowsMenu:windowMenu]; 191 | 192 | /* Finally give up our references to the objects */ 193 | [windowMenu release]; 194 | [windowMenuItem release]; 195 | } 196 | 197 | /* Replacement for NSApplicationMain */ 198 | static void CustomApplicationMain (int argc, char **argv) 199 | { 200 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 201 | SDLMain *sdlMain; 202 | 203 | /* Ensure the application object is initialised */ 204 | [NSApplication sharedApplication]; 205 | 206 | #ifdef SDL_USE_CPS 207 | { 208 | CPSProcessSerNum PSN; 209 | /* Tell the dock about us */ 210 | if (!CPSGetCurrentProcess(&PSN)) 211 | if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) 212 | if (!CPSSetFrontProcess(&PSN)) 213 | [NSApplication sharedApplication]; 214 | } 215 | #endif /* SDL_USE_CPS */ 216 | 217 | /* Set up the menubar */ 218 | [NSApp setMainMenu:[[NSMenu alloc] init]]; 219 | setApplicationMenu(); 220 | setupWindowMenu(); 221 | 222 | /* Create SDLMain and make it the app delegate */ 223 | sdlMain = [[SDLMain alloc] init]; 224 | [NSApp setDelegate:sdlMain]; 225 | 226 | /* Start the main event loop */ 227 | [NSApp run]; 228 | 229 | [sdlMain release]; 230 | [pool release]; 231 | } 232 | 233 | #endif 234 | 235 | 236 | /* 237 | * Catch document open requests...this lets us notice files when the app 238 | * was launched by double-clicking a document, or when a document was 239 | * dragged/dropped on the app's icon. You need to have a 240 | * CFBundleDocumentsType section in your Info.plist to get this message, 241 | * apparently. 242 | * 243 | * Files are added to gArgv, so to the app, they'll look like command line 244 | * arguments. Previously, apps launched from the finder had nothing but 245 | * an argv[0]. 246 | * 247 | * This message may be received multiple times to open several docs on launch. 248 | * 249 | * This message is ignored once the app's mainline has been called. 250 | */ 251 | - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename 252 | { 253 | const char *temparg; 254 | size_t arglen; 255 | char *arg; 256 | char **newargv; 257 | 258 | if (!gFinderLaunch) /* MacOS is passing command line args. */ 259 | return FALSE; 260 | 261 | if (gCalledAppMainline) /* app has started, ignore this document. */ 262 | return FALSE; 263 | 264 | temparg = [filename UTF8String]; 265 | arglen = SDL_strlen(temparg) + 1; 266 | arg = (char *) SDL_malloc(arglen); 267 | if (arg == NULL) 268 | return FALSE; 269 | 270 | newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); 271 | if (newargv == NULL) 272 | { 273 | SDL_free(arg); 274 | return FALSE; 275 | } 276 | gArgv = newargv; 277 | 278 | SDL_strlcpy(arg, temparg, arglen); 279 | gArgv[gArgc++] = arg; 280 | gArgv[gArgc] = NULL; 281 | return TRUE; 282 | } 283 | 284 | 285 | /* Called when the internal event loop has just started running */ 286 | - (void) applicationDidFinishLaunching: (NSNotification *) note 287 | { 288 | int status; 289 | 290 | /* Set the working directory to the .app's parent directory */ 291 | [self setupWorkingDirectory:gFinderLaunch]; 292 | 293 | #if SDL_USE_NIB_FILE 294 | /* Set the main menu to contain the real app name instead of "SDL App" */ 295 | [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; 296 | #endif 297 | 298 | /* Hand off to main application code */ 299 | gCalledAppMainline = TRUE; 300 | status = SDL_main (gArgc, gArgv); 301 | 302 | /* We're done, thank you for playing */ 303 | exit(status); 304 | } 305 | @end 306 | 307 | 308 | @implementation NSString (ReplaceSubString) 309 | 310 | - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString 311 | { 312 | unsigned int bufferSize; 313 | unsigned int selfLen = [self length]; 314 | unsigned int aStringLen = [aString length]; 315 | unichar *buffer; 316 | NSRange localRange; 317 | NSString *result; 318 | 319 | bufferSize = selfLen + aStringLen - aRange.length; 320 | buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar)); 321 | 322 | /* Get first part into buffer */ 323 | localRange.location = 0; 324 | localRange.length = aRange.location; 325 | [self getCharacters:buffer range:localRange]; 326 | 327 | /* Get middle part into buffer */ 328 | localRange.location = 0; 329 | localRange.length = aStringLen; 330 | [aString getCharacters:(buffer+aRange.location) range:localRange]; 331 | 332 | /* Get last part into buffer */ 333 | localRange.location = aRange.location + aRange.length; 334 | localRange.length = selfLen - localRange.location; 335 | [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; 336 | 337 | /* Build output string */ 338 | result = [NSString stringWithCharacters:buffer length:bufferSize]; 339 | 340 | NSDeallocateMemoryPages(buffer, bufferSize); 341 | 342 | return result; 343 | } 344 | 345 | @end 346 | 347 | 348 | 349 | #ifdef main 350 | # undef main 351 | #endif 352 | 353 | 354 | /* Main entry point to executable - should *not* be SDL_main! */ 355 | int main (int argc, char **argv) 356 | { 357 | /* Copy the arguments into a global variable */ 358 | /* This is passed if we are launched by double-clicking */ 359 | if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { 360 | gArgv = (char **) SDL_malloc(sizeof (char *) * 2); 361 | gArgv[0] = argv[0]; 362 | gArgv[1] = NULL; 363 | gArgc = 1; 364 | gFinderLaunch = YES; 365 | } else { 366 | int i; 367 | gArgc = argc; 368 | gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1)); 369 | for (i = 0; i <= argc; i++) 370 | gArgv[i] = argv[i]; 371 | gFinderLaunch = NO; 372 | } 373 | 374 | #if SDL_USE_NIB_FILE 375 | NSApplicationMain (argc, argv); 376 | #else 377 | CustomApplicationMain (argc, argv); 378 | #endif 379 | return 0; 380 | } 381 | 382 | --------------------------------------------------------------------------------