comparison client/main.cpp @ 0:8991989c4708

Initial import
author David Demelier <markand@malikania.fr>
date Tue, 22 Mar 2016 18:26:05 +0100
parents
children e33b246ac2d3
comparison
equal deleted inserted replaced
-1:000000000000 0:8991989c4708
1 /*
2 * main.cpp -- main client files
3 *
4 * Copyright (c) 2014 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <iostream>
20 #include <chrono>
21 #include <thread>
22 #include <map>
23 #include <malikania/Json.h>
24 #include <malikania/Window.h>
25 #include <malikania/Size.h>
26 #include <malikania/Sprite.h>
27 #include <malikania/Image.h>
28 #include <malikania/Point.h>
29 #include <malikania/Label.h>
30 #include <malikania/Animation.h>
31 #include <malikania/Animator.h>
32
33 #if 0
34
35 using namespace std::literals::chrono_literals;
36
37 // TODO delete this... just for fun
38 bool goRight = true;
39 bool goDown = true;
40 const int mokoSize = 300;
41
42 void bounce(malikania::Window& window, int &x, int &y) {
43 malikania::Size resolution = window.getWindowResolution();
44 int width = resolution.width;
45 int height = resolution.height;
46 if (y < 10) {
47 goDown = true;
48 y += 1;
49 }
50 if (goDown && y < height - mokoSize) {
51 // Moko falls
52 y += 0.2 * y;
53 } else {
54 // Moko will bounce!!!
55 goDown = false;
56 }
57 if (!goDown && y > 0) {
58 y -= 0.1 * y;
59 } else {
60 goDown = true;
61 }
62
63 if (goRight && x < width - mokoSize) {
64 x += 4;
65 } else {
66 goRight = false;
67 }
68 if (!goRight && x > 0) {
69 x -= 4;
70 } else {
71 goRight = true;
72 }
73 }
74
75 // End TODO
76
77 int main(void)
78 {
79 malikania::Window mainWindow;
80
81 bool isBouncing = false;
82
83 int mokoPositionX = 0;
84 int mokoPositionY = 0;
85
86 std::map<int, bool> keyPressed = { {SDLK_UP, false}, {SDLK_DOWN, false}, {SDLK_RIGHT, false}, {SDLK_LEFT, false} };
87
88 mainWindow.setOnKeyDown([&mainWindow, &mokoPositionX, &mokoPositionY, &isBouncing, &keyPressed](int sdlKey) {
89 switch (sdlKey) {
90 case SDLK_ESCAPE:
91 mainWindow.close();
92 break;
93 case SDLK_UP:
94 keyPressed[SDLK_UP] = true;
95 break;
96 case SDLK_DOWN:
97 keyPressed[SDLK_DOWN] = true;
98 break;
99 case SDLK_RIGHT:
100 keyPressed[SDLK_RIGHT] = true;
101 break;
102 case SDLK_LEFT:
103 keyPressed[SDLK_LEFT] = true;
104 break;
105 case SDLK_m:
106 isBouncing = !isBouncing;
107 break;
108 }
109 });
110
111 mainWindow.setOnKeyUp([&keyPressed](int sdlKey) {
112 switch (sdlKey) {
113 case SDLK_UP:
114 keyPressed[SDLK_UP] = false;
115 break;
116 case SDLK_DOWN:
117 keyPressed[SDLK_DOWN] = false;
118 break;
119 case SDLK_RIGHT:
120 keyPressed[SDLK_RIGHT] = false;
121 break;
122 case SDLK_LEFT:
123 keyPressed[SDLK_LEFT] = false;
124 break;
125 }
126 });
127
128 int animationStep = 1;
129 mainWindow.setOnRefresh([&mainWindow, &keyPressed, &animationStep](){
130 if (keyPressed[SDLK_LEFT]) {
131 std::string animationState = "left" + std::to_string(animationStep > 4 ? 4 : animationStep++);
132 } else if (keyPressed[SDLK_RIGHT]) {
133 std::string animationState = "right" + std::to_string(animationStep > 4 ? 4 : animationStep++);
134 } else if (keyPressed[SDLK_DOWN]) {
135 std::string animationState = "down" + std::to_string(animationStep > 4 ? 4 : animationStep++);
136 } else {
137 animationStep = 1;
138 }
139 });
140
141 malikania::Sprite testSprite = malikania::Sprite::fromJson(mainWindow, malikania::JsonDocument(
142 "{\"image\": \"resources/images/mokodemo.png\", \"alias\": \"testSprite\", \"cell\": [300, 300], \"size\": [1200, 900]}"
143 ).toObject());
144
145 std::shared_ptr<malikania::Font> font = std::make_shared<malikania::Font>("resources/fonts/DejaVuSans.ttf", 48);
146 malikania::Label testLabel("Malikania !!! Youpi !", font, {0, 0, 100, 50});
147
148 std::shared_ptr<malikania::Animation> testAnimation = std::make_shared<malikania::Animation>(malikania::Animation::fromJson(mainWindow, malikania::JsonDocument(
149 std::string("{\"sprite\": \"no-working-yet.json\", \"alias\": \"testAnimation\", \"frames\": [")
150 + "{ \"delay\": 200, \"cell\": 0 }, { \"delay\": 10, \"cell\": 1 },"
151 + "{ \"delay\": 10, \"cell\": 2 }, { \"delay\": 200, \"cell\": 3 },"
152 + "{ \"delay\": 10, \"cell\": 1 }, { \"delay\": 10, \"cell\": 1 },"
153 + "{ \"delay\": 200, \"cell\": 4 }, { \"delay\": 10, \"cell\": 5 },"
154 + "{ \"delay\": 10, \"cell\": 6 }, { \"delay\": 200, \"cell\": 7 },"
155 + "{ \"delay\": 10, \"cell\": 6 }, { \"delay\": 10, \"cell\": 5 },"
156 + "{ \"delay\": 200, \"cell\": 8 }, { \"delay\": 10, \"cell\": 9 },"
157 + "{ \"delay\": 10, \"cell\": 10 }, { \"delay\": 200, \"cell\": 11 },"
158 + "{ \"delay\": 10, \"cell\": 10 }, { \"delay\": 10, \"cell\": 9 }"
159 + "]}"
160 ).toObject()));
161
162 std::shared_ptr<malikania::Animation> testAnimation2 = std::make_shared<malikania::Animation>(malikania::Animation::fromJson(mainWindow, malikania::JsonDocument(
163 std::string("{\"sprite\": \"no-working-yet.json\", \"alias\": \"testAnimation\", \"frames\": [")
164 + "{ \"delay\": 2000, \"cell\": 0 }, { \"delay\": 10, \"cell\": 1 },"
165 + "{ \"delay\": 10, \"cell\": 2 }, { \"delay\": 2000, \"cell\": 3 },"
166 + "{ \"delay\": 10, \"cell\": 1 }, { \"delay\": 10, \"cell\": 1 }"
167 + "]}"
168 ).toObject()));
169
170 malikania::Animator testAnimator1 = malikania::Animator(testAnimation);
171 malikania::Animator testAnimator2 = malikania::Animator(std::move(testAnimation));
172 malikania::Animator testAnimator3 = malikania::Animator(std::move(testAnimation2));
173
174 while (mainWindow.isOpen()) {
175
176 // TODO delete this, just for fun...
177 if (isBouncing) {
178 bounce(mainWindow, mokoPositionX, mokoPositionY);
179 }
180
181 mainWindow.processEvent();
182 mainWindow.setDrawingColor({255, 255, 255, 255});
183 mainWindow.clear();
184 mainWindow.update();
185
186 testSprite.draw(mainWindow, 0, {0, 0, 300, 300});
187 testSprite.draw(mainWindow, 1, {200, 200, 300, 300});
188 testSprite.draw(mainWindow, 2, {400, 400, 300, 300});
189 testSprite.draw(mainWindow, 11, {600, 400, 300, 300});
190
191 malikania::Color c{255, 50, 40, 255};
192 mainWindow.setDrawingColor(c);
193 mainWindow.drawLine({0, 0, 300, 300});
194
195 std::vector<malikania::Point> points{{20, 20}, {30, 50}, {100, 200}, {30, 60}, {20, 300}, {100, 20}};
196 mainWindow.drawLines(points);
197
198 mainWindow.setDrawingColor({200, 50, 200, 255});
199 mainWindow.drawPoint({400, 400});
200 mainWindow.drawPoint({400, 402});
201 mainWindow.drawPoint({400, 405});
202 mainWindow.drawPoint({400, 407});
203 mainWindow.drawPoint({400, 410});
204
205 mainWindow.setDrawingColor({0, 0, 0, 255});
206 mainWindow.drawPoints(points);
207
208 mainWindow.setDrawingColor({30, 30, 30, 255});
209 mainWindow.drawRectangle({500, 500, 200, 100});
210
211 mainWindow.setDrawingColor({130, 30, 30, 255});
212 mainWindow.drawRectangles({{800, 800, 200, 100}, {700, 700, 200, 100}, {750, 750, 200, 100}});
213
214 mainWindow.drawRectangle({600, 200, 200, 100}, true, {0, 255, 0, 255});
215
216 mainWindow.drawRectangles(
217 {{800, 400, 200, 100}, {700, 450, 200, 100}, {750, 500, 200, 100}},
218 true,
219 {{255,0,0,255},{0,255,0,255},{0,0,255,255}}
220 );
221
222 testLabel.draw(mainWindow, {300, 300, 200, 50});
223
224 testAnimator1.draw(mainWindow, {1000, 0, 300, 300});
225 testAnimator2.draw(mainWindow, {100, 600, 300, 300});
226 testAnimator3.draw(mainWindow, {400, 600, 300, 300});
227
228 mainWindow.present();
229
230 std::this_thread::sleep_for(5ms);
231 }
232
233 //malikania::Window::quit();
234
235 return 0;
236 }
237
238 #endif
239
240 int main() { return 0; }