2
This file is part of the Tweeny library.
4
Copyright (c) 2016-2017 Leonardo G. Lucena de Freitas
5
Copyright (c) 2016 Guilherme R. Costa
7
Permission is hereby granted, free of charge, to any person obtaining a copy of
8
this software and associated documentation files (the "Software"), to deal in
9
the Software without restriction, including without limitation the rights to
10
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
the Software, and to permit persons to whom the Software is furnished to do so,
12
subject to the following conditions:
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
/* This example uses tweens to control the frames of a sprite */
32
#include <emscripten/emscripten.h>
35
/* References to resources in resources.c */
36
extern const unsigned char zombie_0_png[];
37
extern const unsigned int zombie_0_png_len;
38
extern const unsigned char arrows_to_walk_png[];
39
extern const unsigned int arrows_to_walk_png_len;
41
/* Forward declaration of loop and yoyo functions */
42
bool loop(tweeny::tween<int> & t, int);
43
bool yoyo(tweeny::tween<int> & t, int);
45
/* Enumerations representing which stripe should be used in the zombie sprite */
57
/* The state represents the "game state": it contains sprites, tweens and other
58
* variables. It also has the run() function that is called in each frame */
60
ex::engine engine = ex::engine(800, 600); /* creates the window and initializes SDL */
61
ex::sprite text = engine.sprite(arrows_to_walk_png, arrows_to_walk_png_len, 1, 1); /* help text */
65
int direction = SPRITE_RIGHT;
69
tweeny::tween<int> idle = tweeny::from(0).to(3).during(1000).onStep(yoyo); /* idle animation */
70
tweeny::tween<int> walking = tweeny::from(4).to(11).during(1000).onStep(loop); /* walking animation */
71
tweeny::tween<int> attacking = tweeny::from(12).to(15).during(300).to(14).during(300).onStep([this](tweeny::tween<int> & t, int) {
72
/* attacking animation. when ended, stops the attack */
73
if (t.progress() >= 1.0f) { attack = false; }
76
zombie(ex::engine & engine) : sprite(engine.sprite(zombie_0_png, zombie_0_png_len, 36, 8)) { }
81
engine.clear(93, 77, 157); /* clear the screen */
82
process(); /* process all input */
84
/* decides which animation should be running */
85
if (z.attack) z.frame = z.attacking.step(engine.dt);
87
if (ex::input::pressed()) z.frame = z.walking.step(engine.dt);
88
else z.frame = z.idle.step(engine.dt);
91
text.render(10, 10); /* render help text */
92
z.sprite.render(z.frame, z.direction, z.x, z.y); /* render zombie sprite */
97
/* process the input state and sets attacking state */
99
if (ex::input::up()) { z.direction = SPRITE_UP; if (!z.attack) z.y -= 1.0f; }
100
if (ex::input::down()) { z.direction = SPRITE_DOWN; if (!z.attack) z.y += 1.0f; }
101
if (ex::input::left()) { z.direction = SPRITE_LEFT; if (!z.attack) z.x -= 1.0f; }
102
if (ex::input::right()) { z.direction = SPRITE_RIGHT; if (!z.attack) z.x += 1.0f; }
103
if (ex::input::up() && ex::input::right()) z.direction = SPRITE_RIGHTUP;
104
if (ex::input::right() && ex::input::down()) z.direction = SPRITE_RIGHTDOWN;
105
if (ex::input::down() && ex::input::left()) z.direction = SPRITE_LEFTDOWN;
106
if (ex::input::up() && ex::input::left()) z.direction = SPRITE_LEFTUP;
107
if (ex::input::space()) {
113
bool quit() { return engine.quit(); }
116
void iterate(state * s) { s->run(); }
118
int main(int argc, char ** argv) {
121
emscripten_set_main_loop_arg((em_arg_callback_func)iterate, &s, 0, 1);
123
while (!s.quit()) iterate(&s);
128
bool yoyo(tweeny::tween<int> & t, int) {
129
if (t.progress() >= 1.0f) t.backward();
130
if (t.progress() <= 0.0f) t.forward();
135
bool loop(tweeny::tween<int> & t, int) {
136
if (t.progress() >= 1.0f) t.seek(0);
140
#define STB_IMAGE_IMPLEMENTATION
141
#include "stb_image.h"