~ubuntu-branches/debian/sid/tweeny/sid

« back to all changes in this revision

Viewing changes to examples/sprite/main.cpp

  • Committer: Package Import Robot
  • Author(s): Hubert Chathi
  • Date: 2018-04-04 17:06:02 UTC
  • Revision ID: package-import@ubuntu.com-20180404170602-gpp0u0k782h61u33
Tags: upstream-2+git20171120.b94ce07
ImportĀ upstreamĀ versionĀ 2+git20171120.b94ce07

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 This file is part of the Tweeny library.
 
3
 
 
4
 Copyright (c) 2016-2017 Leonardo G. Lucena de Freitas
 
5
 Copyright (c) 2016 Guilherme R. Costa
 
6
 
 
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:
 
13
 
 
14
 The above copyright notice and this permission notice shall be included in all
 
15
 copies or substantial portions of the Software.
 
16
 
 
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.
 
23
*/
 
24
 
 
25
/* This example uses tweens to control the frames of a sprite */
 
26
 
 
27
#include "tweeny.h"
 
28
#include "sprite.h"
 
29
#include "engine.h"
 
30
#include "input.h"
 
31
#ifdef EMSCRIPTEN
 
32
#include <emscripten/emscripten.h>
 
33
#endif
 
34
 
 
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;
 
40
 
 
41
/* Forward declaration of loop and yoyo functions */
 
42
bool loop(tweeny::tween<int> & t, int);
 
43
bool yoyo(tweeny::tween<int> & t, int);
 
44
 
 
45
/* Enumerations representing which stripe should be used in the zombie sprite */
 
46
enum {
 
47
    SPRITE_LEFT = 0,
 
48
    SPRITE_LEFTUP = 1,
 
49
    SPRITE_UP = 2,
 
50
    SPRITE_RIGHTUP = 3,
 
51
    SPRITE_RIGHT = 4,
 
52
    SPRITE_RIGHTDOWN = 5,
 
53
    SPRITE_DOWN = 6,
 
54
    SPRITE_LEFTDOWN = 7
 
55
};
 
56
 
 
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 */
 
59
struct state {
 
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 */
 
62
    struct zombie {
 
63
        float x = 0;
 
64
        float y = 0;
 
65
        int direction = SPRITE_RIGHT;
 
66
        bool attack = false;
 
67
        int frame = 0;
 
68
        ex::sprite sprite;
 
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; }
 
74
            return false;
 
75
        });
 
76
        zombie(ex::engine & engine) : sprite(engine.sprite(zombie_0_png, zombie_0_png_len, 36, 8)) { }
 
77
    } z = zombie(engine);
 
78
 
 
79
    /* Run a frame */
 
80
    void run() {
 
81
        engine.clear(93, 77, 157); /* clear the screen */
 
82
        process(); /* process all input */
 
83
 
 
84
        /* decides which animation should be running */
 
85
        if (z.attack) z.frame = z.attacking.step(engine.dt);
 
86
        else {
 
87
            if (ex::input::pressed()) z.frame = z.walking.step(engine.dt);
 
88
            else z.frame = z.idle.step(engine.dt);
 
89
        }
 
90
 
 
91
        text.render(10, 10); /* render help text */
 
92
        z.sprite.render(z.frame, z.direction, z.x, z.y); /* render zombie sprite */
 
93
        engine.delay(16);
 
94
        engine.flip();
 
95
    }
 
96
 
 
97
    /* process the input state and sets attacking state */
 
98
    void process() {
 
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()) {
 
108
            z.attacking.seek(0);
 
109
            z.attack = true;
 
110
        }
 
111
    }
 
112
 
 
113
    bool quit() { return engine.quit(); }
 
114
};
 
115
 
 
116
void iterate(state * s) { s->run(); }
 
117
 
 
118
int main(int argc, char ** argv) {
 
119
    state s;
 
120
    #ifdef EMSCRIPTEN
 
121
    emscripten_set_main_loop_arg((em_arg_callback_func)iterate, &s, 0, 1);
 
122
    #else
 
123
    while (!s.quit()) iterate(&s);
 
124
    #endif
 
125
}
 
126
 
 
127
/* yoyo's a tween */
 
128
bool yoyo(tweeny::tween<int> & t, int) {
 
129
    if (t.progress() >= 1.0f) t.backward();
 
130
    if (t.progress() <= 0.0f) t.forward();
 
131
    return false;
 
132
}
 
133
 
 
134
/* loop a tween */
 
135
bool loop(tweeny::tween<int> & t, int) {
 
136
    if (t.progress() >= 1.0f) t.seek(0);
 
137
    return false;
 
138
}
 
139
 
 
140
#define STB_IMAGE_IMPLEMENTATION
 
141
#include "stb_image.h"
 
142
 
 
143