Browse code

rename game to screen

devnewton authored on 09/09/2015 19:34:48
Showing 13 changed files
... ...
@@ -32,7 +32,7 @@ include_directories(${SDL2_TTF_INCLUDE_DIR})
32 32
 add_executable(superpaflaballe
33 33
  src/bourrines_benchmark.cpp
34 34
  src/scenimp/assets.cpp
35
- src/scenimp/game.cpp
35
+ src/scenimp/screen.cpp
36 36
  src/scenimp/group.cpp
37 37
  src/scenimp/nanim.cpp
38 38
  src/scenimp/node.cpp
... ...
@@ -52,6 +52,10 @@ namespace superpaflaballe {
52 52
     class move_system : public bourrines::selective_processing_system<world> {
53 53
     public:
54 54
 
55
+        move_system(scenimp::screen& s)
56
+        : screen_(s) {
57
+        }
58
+
55 59
         virtual bool accept(bourrines::entity e) override {
56 60
             return has<pos_component>(e) && has<dir_component>(e);
57 61
         }
... ...
@@ -71,22 +75,25 @@ namespace superpaflaballe {
71 75
                 dir.dy_ = -dir.dy_;
72 76
             }
73 77
 
74
-            if (pos.x_ >= scenimp::logical_screen_width) {
78
+            if (pos.x_ >= screen_.logical_screen_width()) {
75 79
                 dir.dx_ = -dir.dx_;
76 80
             }
77 81
 
78
-            if (pos.y_ >= scenimp::logical_screen_height) {
82
+            if (pos.y_ >= screen_.logical_screen_height()) {
79 83
                 dir.dy_ = -dir.dy_;
80 84
             }
81 85
         }
82 86
 
87
+    private:
88
+        scenimp::screen& screen_;
89
+
83 90
     };
84 91
 
85 92
     class render_system : public bourrines::selective_processing_system<world> {
86 93
     public:
87 94
 
88
-        render_system(scenimp::game& g)
89
-        : game_(g) {
95
+        render_system(scenimp::screen& s)
96
+        : screen_(s) {
90 97
         }
91 98
 
92 99
         virtual bool accept(bourrines::entity e) override {
... ...
@@ -101,20 +108,20 @@ namespace superpaflaballe {
101 108
             rect.h = 32;
102 109
             rect.x = pos.x_;
103 110
             rect.y = pos.y_;
104
-            SDL_RenderCopy(game_.renderer(), anim.play_->current_frame().image().get(), &anim.play_->current_frame().rect(), &rect);
111
+            SDL_RenderCopy(screen_.renderer(), anim.play_->current_frame().image().get(), &anim.play_->current_frame().rect(), &rect);
105 112
         }
106 113
     private:
107
-        scenimp::game& game_;
114
+        scenimp::screen& screen_;
108 115
     };
109 116
 
110
-    bourrines_benchmark::bourrines_benchmark(scenimp::game& ga, int num_entity, int num_ticks)
111
-    : remaining_ticks_(num_ticks) {
117
+    bourrines_benchmark::bourrines_benchmark(scenimp::screen& screen, int num_entity, int num_ticks)
118
+    : screen_(screen), remaining_ticks_(num_ticks) {
112 119
         timer_.stop();
113
-        ned_anim_ = ga.assets().animations("ned.json");
120
+        ned_anim_ = screen.assets().animations("ned.json");
114 121
         world_.add_system(1, std::unique_ptr<hera_system>(new hera_system(*this, num_entity)));
115 122
         world_.add_system(2, std::unique_ptr<hades_system>(new hades_system()));
116
-        world_.add_system(3, std::unique_ptr<move_system>(new move_system()));
117
-        world_.add_system(4, std::unique_ptr<render_system>(new render_system(ga)));
123
+        world_.add_system(3, std::unique_ptr<move_system>(new move_system(screen)));
124
+        world_.add_system(4, std::unique_ptr<render_system>(new render_system(screen)));
118 125
     }
119 126
 
120 127
     bourrines_benchmark::~bourrines_benchmark() {
... ...
@@ -124,8 +131,8 @@ namespace superpaflaballe {
124 131
     void bourrines_benchmark::create_ned() {
125 132
         bourrines::entity e = world_.create_entity();
126 133
         pos_component& pos = world_.add<pos_component>(e);
127
-        pos.x_ = std::rand() % scenimp::logical_screen_width;
128
-        pos.y_ = std::rand() % scenimp::logical_screen_height;
134
+        pos.x_ = std::rand() % screen_.logical_screen_width();
135
+        pos.y_ = std::rand() % screen_.logical_screen_height();
129 136
 
130 137
         dir_component& dir = world_.add<dir_component>(e);
131 138
         dir.dx_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
... ...
@@ -1,6 +1,6 @@
1 1
 #pragma once
2 2
 
3
-#include "scenimp/game.h"
3
+#include "scenimp/screen.h"
4 4
 #include "scenimp/nanim.h"
5 5
 #include "scenimp/assets.h"
6 6
 #include "bourrines/bourrines.h"
... ...
@@ -29,7 +29,7 @@ namespace superpaflaballe {
29 29
 
30 30
     class bourrines_benchmark {
31 31
     public:
32
-        bourrines_benchmark(scenimp::game& g, int num_entity, int num_ticks);
32
+        bourrines_benchmark(scenimp::screen& g, int num_entity, int num_ticks);
33 33
         ~bourrines_benchmark();
34 34
         
35 35
         void tick();
... ...
@@ -39,6 +39,7 @@ namespace superpaflaballe {
39 39
         
40 40
     private:
41 41
         world world_;
42
+        scenimp::screen& screen_;
42 43
         std::shared_ptr< scenimp::nanim::collection > ned_anim_;
43 44
         boost::timer::auto_cpu_timer timer_;
44 45
         int remaining_ticks_;
... ...
@@ -7,8 +7,8 @@
7 7
 
8 8
 namespace superpaflaballe {
9 9
 
10
-    intro::intro(scenimp::game& ga)
11
-    : game_(ga)
10
+    intro::intro(scenimp::screen& ga)
11
+    : screen_(ga)
12 12
     , play_(ga.assets().animations("intro/devnewton.json")->play(scenimp::nanim::once))
13 13
     , music_(ga.assets().music("intro/devnewton.ogg")) {
14 14
         Mix_PlayMusic(music_.get(), 1);
... ...
@@ -23,9 +23,9 @@ namespace superpaflaballe {
23 23
         SDL_Rect rect;
24 24
         rect.w = 512;
25 25
         rect.h = 116;
26
-        rect.x = (scenimp::logical_screen_width - rect.w) / 2;
27
-        rect.y = (scenimp::logical_screen_height - rect.h) / 2;
28
-        SDL_RenderCopy(game_.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect);
26
+        rect.x = (screen_.logical_screen_width() - rect.w) / 2;
27
+        rect.y = (screen_.logical_screen_height() - rect.h) / 2;
28
+        SDL_RenderCopy(screen_.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect);
29 29
     }
30 30
 
31 31
     bool intro::is_finished() {
... ...
@@ -1,6 +1,6 @@
1 1
 #pragma once
2 2
 
3
-#include "scenimp/game.h"
3
+#include "scenimp/screen.h"
4 4
 #include "scenimp/nanim.h"
5 5
 #include "scenimp/assets.h"
6 6
 
... ...
@@ -8,12 +8,12 @@ namespace superpaflaballe {
8 8
 
9 9
     class intro {
10 10
     public:
11
-        intro(scenimp::game& g);
11
+        intro(scenimp::screen& g);
12 12
         ~intro();
13 13
         void tick();
14 14
         bool is_finished();
15 15
     private:
16
-        scenimp::game& game_;
16
+        scenimp::screen& screen_;
17 17
         std::shared_ptr<scenimp::nanim::play> play_;
18 18
         std::shared_ptr<Mix_Music> music_;
19 19
     };
... ...
@@ -1,5 +1,5 @@
1 1
 #include "scenimp/assets.h"
2
-#include "scenimp/game.h"
2
+#include "scenimp/screen.h"
3 3
 #include "intro.h"
4 4
 #include "bourrines_benchmark.h"
5 5
 #include <boost/statechart/state_machine.hpp>
... ...
@@ -24,7 +24,7 @@ namespace superpaflaballe {
24 24
         struct state_outro;
25 25
 
26 26
         struct machine : boost::statechart::state_machine< machine, state_running > {
27
-            scenimp::game game_;
27
+            scenimp::screen screen_;
28 28
         };
29 29
 
30 30
         struct state_running : boost::statechart::simple_state< state_running, machine, state_intro > {
... ...
@@ -47,7 +47,7 @@ namespace superpaflaballe {
47 47
 
48 48
             state_intro(my_context ctx)
49 49
             : boost::statechart::state< state_intro, state_running >(ctx)
50
-            , intro_(this->outermost_context().game_) {
50
+            , intro_(this->outermost_context().screen_) {
51 51
             }
52 52
 
53 53
             boost::statechart::result react(const event_tick&) {
... ...
@@ -74,7 +74,7 @@ namespace superpaflaballe {
74 74
 
75 75
             state_benchmark(my_context ctx)
76 76
             : boost::statechart::state< state_benchmark, state_running >(ctx)
77
-            , benchmark_(this->outermost_context().game_, 10000, 60 * 10) {
77
+            , benchmark_(this->outermost_context().screen_, 10000, 60 * 10) {
78 78
             }
79 79
 
80 80
             boost::statechart::result react(const event_tick&) {
... ...
@@ -104,8 +104,8 @@ int main(int, char**) {
104 104
 
105 105
         superpaflaballe::statechart::event_sdl event_sdl;
106 106
         superpaflaballe::statechart::event_tick event_tick;
107
-        auto font = machine.game_.assets().font("ProFontWindows.ttf", 12);
108
-        auto text_texture = machine.game_.create_text_texture(font, "plop");
107
+        auto font = machine.screen_.assets().font("ProFontWindows.ttf", 12);
108
+        auto text_texture = machine.screen_.create_text_texture(font, "plop");
109 109
         
110 110
         FPSmanager fps;
111 111
         SDL_initFramerate(&fps);
... ...
@@ -114,10 +114,10 @@ int main(int, char**) {
114 114
             while (SDL_PollEvent(&event_sdl.e_)) {
115 115
                 machine.process_event(event_sdl);
116 116
             }
117
-            SDL_RenderClear(machine.game_.renderer());
117
+            SDL_RenderClear(machine.screen_.renderer());
118 118
             machine.process_event(event_tick);
119
-            SDL_RenderCopy(machine.game_.renderer(), text_texture.get(), nullptr, nullptr);
120
-            SDL_RenderPresent(machine.game_.renderer());
119
+            SDL_RenderCopy(machine.screen_.renderer(), text_texture.get(), nullptr, nullptr);
120
+            SDL_RenderPresent(machine.screen_.renderer());
121 121
             SDL_framerateDelay(&fps);
122 122
         }
123 123
     } catch (const std::exception& e) {
... ...
@@ -1,5 +1,5 @@
1 1
 #include "assets.h"
2
-#include "game.h"
2
+#include "screen.h"
3 3
 #include "sdl_exception.h"
4 4
 #include <SDL_image.h>
5 5
 #include <boost/property_tree/ptree.hpp>
... ...
@@ -9,8 +9,8 @@
9 9
 
10 10
 namespace scenimp {
11 11
 
12
-    assets::assets(game& game)
13
-    : game_(game) {
12
+    assets::assets(screen& screen)
13
+    : screen_(screen) {
14 14
         pathPrefixes_.push_back("../assets/");
15 15
         pathPrefixes_.push_back(PREFIX "/games/superpaflaballe/");
16 16
         if (TTF_Init() != 0) {
... ...
@@ -99,7 +99,7 @@ namespace scenimp {
99 99
     }
100 100
 
101 101
     std::shared_ptr< SDL_Texture > assets::load_texture(const std::string path) {
102
-        return std::shared_ptr< SDL_Texture >(IMG_LoadTexture(game_.renderer(), path.c_str()), SDL_DestroyTexture);
102
+        return std::shared_ptr< SDL_Texture >(IMG_LoadTexture(screen_.renderer(), path.c_str()), SDL_DestroyTexture);
103 103
     }
104 104
 
105 105
     std::shared_ptr< nanim::collection > assets::animations(const std::string& path) {
... ...
@@ -6,11 +6,11 @@
6 6
 
7 7
 namespace scenimp {
8 8
 
9
-    class game;
9
+    class screen;
10 10
 
11 11
     class assets {
12 12
     public:
13
-        assets(game& game);
13
+        assets(screen& s);
14 14
         ~assets();
15 15
 
16 16
         std::shared_ptr< nanim::collection > animations(const std::string& path);
... ...
@@ -25,7 +25,7 @@ namespace scenimp {
25 25
         std::shared_ptr< Mix_Music > load_music(const std::string& path);
26 26
         std::shared_ptr< SDL_Texture > load_texture(const std::string path);
27 27
 
28
-        game& game_;
28
+        screen& screen_;
29 29
         std::vector<std::string> pathPrefixes_;
30 30
         std::map<std::string, std::weak_ptr< nanim::collection > > animations_;
31 31
         std::map<std::pair<std::string, int/*font size*/>, std::weak_ptr< TTF_Font > > fonts_;
32 32
deleted file mode 100644
... ...
@@ -1,51 +0,0 @@
1
-#include "game.h"
2
-#include "sdl_exception.h"
3
-
4
-#include <SDL.h>
5
-
6
-namespace scenimp {
7
-
8
-    game::game() {
9
-        if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
10
-            throw sdl_exception();
11
-        }
12
-        window_ = SDL_CreateWindow("superpaflaballe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 360, SDL_WINDOW_RESIZABLE);
13
-        if (!window_) {
14
-            SDL_Quit();
15
-            throw sdl_exception();
16
-        }
17
-        renderer_ = SDL_CreateRenderer(window_, -1, 0);
18
-        if (!renderer_) {
19
-            SDL_DestroyWindow(window_);
20
-            SDL_Quit();
21
-            throw sdl_exception();
22
-        }
23
-        SDL_RenderSetLogicalSize(renderer_, logical_screen_width, logical_screen_height);
24
-        assets_ = new scenimp::assets(*this);
25
-    }
26
-
27
-    game::~game() {
28
-        delete assets_;
29
-        SDL_DestroyRenderer(renderer_);
30
-        SDL_DestroyWindow(window_);
31
-        SDL_Quit();
32
-    }
33
-
34
-    SDL_Window* game::window() {
35
-        return window_;
36
-    }
37
-
38
-    SDL_Renderer* game::renderer() {
39
-        return renderer_;
40
-    }
41
-
42
-    assets& game::assets() {
43
-        return *assets_;
44
-    }
45
-
46
-    std::shared_ptr< SDL_Texture > game::create_text_texture(std::shared_ptr< TTF_Font > font, const std::string& str, const SDL_Color& color) {
47
-        std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *) > text_surface(TTF_RenderText_Blended(font.get(), str.c_str(), color), SDL_FreeSurface);
48
-        return std::shared_ptr< SDL_Texture >(SDL_CreateTextureFromSurface(renderer(), text_surface.get()), SDL_DestroyTexture);
49
-    }
50
-
51
-}
52 0
deleted file mode 100644
... ...
@@ -1,28 +0,0 @@
1
-#pragma once
2
-
3
-#include "assets.h"
4
-
5
-namespace scenimp {
6
-
7
-    const int logical_screen_width = 1920;
8
-    const int logical_screen_height = 1080;
9
-
10
-    class game {
11
-    public:
12
-        game();
13
-        ~game();
14
-
15
-        SDL_Window* window();
16
-        SDL_Renderer* renderer();
17
-        scenimp::assets& assets();
18
-
19
-        std::shared_ptr< SDL_Texture > create_text_texture(std::shared_ptr< TTF_Font > font, const std::string& str, const SDL_Color& color = {255, 255, 255, 0});
20
-
21
-    private:
22
-        scenimp::assets* assets_;
23
-        SDL_Window* window_;
24
-        SDL_Renderer* renderer_;
25
-    };
26
-
27
-}
28
-
... ...
@@ -59,7 +59,7 @@ namespace scenimp {
59 59
         class animation : public std::enable_shared_from_this<animation> {
60 60
         public:
61 61
 
62
-            std::shared_ptr<::scenimp::nanim::play> play(play_mode m);
62
+            std::shared_ptr< ::scenimp::nanim::play > play(play_mode m);
63 63
 
64 64
         private:
65 65
             std::vector<frame> frames_;
... ...
@@ -73,7 +73,7 @@ namespace scenimp {
73 73
         public:
74 74
             std::shared_ptr<animation> get(const std::string& name);
75 75
             std::shared_ptr<animation> first();            
76
-            std::shared_ptr<::scenimp::nanim::play> play(play_mode m);
76
+            std::shared_ptr< ::scenimp::nanim::play > play(play_mode m);
77 77
 
78 78
         private:
79 79
             std::map< std::string, std::shared_ptr<animation> > animations_;
80 80
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+#include "screen.h"
2
+#include "sdl_exception.h"
3
+
4
+#include <SDL.h>
5
+
6
+namespace scenimp {
7
+
8
+    screen::screen() {
9
+        if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
10
+            throw sdl_exception();
11
+        }
12
+        window_ = SDL_CreateWindow("superpaflaballe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 360, SDL_WINDOW_RESIZABLE);
13
+        if (!window_) {
14
+            SDL_Quit();
15
+            throw sdl_exception();
16
+        }
17
+        renderer_ = SDL_CreateRenderer(window_, -1, 0);
18
+        if (!renderer_) {
19
+            SDL_DestroyWindow(window_);
20
+            SDL_Quit();
21
+            throw sdl_exception();
22
+        }
23
+        SDL_RenderSetLogicalSize(renderer_, 1920, 1080);
24
+        assets_ = new scenimp::assets(*this);
25
+    }
26
+
27
+    screen::~screen() {
28
+        delete assets_;
29
+        SDL_DestroyRenderer(renderer_);
30
+        SDL_DestroyWindow(window_);
31
+        SDL_Quit();
32
+    }
33
+
34
+    int screen::logical_screen_width() const {
35
+        int w, h;
36
+        SDL_RenderGetLogicalSize(renderer_, &w, &h);
37
+        return w;
38
+    }
39
+
40
+    int screen::logical_screen_height() const {
41
+        int w, h;
42
+        SDL_RenderGetLogicalSize(renderer_, &w, &h);
43
+        return h;
44
+    }
45
+
46
+    SDL_Renderer* screen::renderer() {
47
+        return renderer_;
48
+    }
49
+
50
+    assets& screen::assets() {
51
+        return *assets_;
52
+    }
53
+
54
+    std::shared_ptr< SDL_Texture > screen::create_text_texture(std::shared_ptr< TTF_Font > font, const std::string& str, const SDL_Color& color) {
55
+        std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *) > text_surface(TTF_RenderText_Blended(font.get(), str.c_str(), color), SDL_FreeSurface);
56
+        return std::shared_ptr< SDL_Texture >(SDL_CreateTextureFromSurface(renderer(), text_surface.get()), SDL_DestroyTexture);
57
+    }
58
+
59
+}
0 60
new file mode 100644
... ...
@@ -0,0 +1,27 @@
1
+#pragma once
2
+
3
+#include "assets.h"
4
+
5
+namespace scenimp {
6
+
7
+    class screen {
8
+    public:
9
+        screen();
10
+        ~screen();
11
+
12
+        int logical_screen_width() const;
13
+        int logical_screen_height() const;
14
+
15
+        SDL_Renderer* renderer();
16
+        scenimp::assets& assets();
17
+
18
+        std::shared_ptr< SDL_Texture > create_text_texture(std::shared_ptr< TTF_Font > font, const std::string& str, const SDL_Color& color = {255, 255, 255, 0});
19
+
20
+    private:
21
+        scenimp::assets* assets_;
22
+        SDL_Window* window_;
23
+        SDL_Renderer* renderer_;
24
+    };
25
+
26
+}
27
+