]> Dogcows Code - chaz/yoink/blob - src/moof/sprite.cc
use only triangles; no quads
[chaz/yoink] / src / moof / sprite.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstdio> // FILE
13 #include <cstring> // strncmp
14 #include <stdexcept>
15
16 #include <boost/algorithm/string.hpp>
17 #include <boost/bind.hpp>
18 #include <stlplus/portability/file_system.hpp>
19
20 #include "dispatcher.hh"
21 #include "log.hh"
22 #include "opengl.hh"
23 #include "sprite.hh"
24
25
26 namespace moof {
27
28
29 sprite::sprite(const std::string& name, int tile)
30 {
31 image(name);
32 sprite::tile(tile);
33 }
34
35 sprite::sprite(const image_handle& image, int tile) :
36 image_(image)
37 {
38 sprite::tile(tile);
39 }
40
41 sprite::sprite(const sprite& sprite, int tile)
42 {
43 // FIXME broken
44 image_ = sprite.image_;
45 }
46
47
48 void sprite::image(const std::string& name)
49 {
50 image_ = resource::load(name, "png");
51 }
52
53 void sprite::tile(int tile)
54 {
55 if (image_) image_->tile_coordinates(tile, tile_);
56 }
57
58
59 void sprite::bind() const
60 {
61 if (image_) image_->bind();
62 }
63
64 void sprite::reset_binding()
65 {
66 image::reset_binding();
67 }
68
69
70 void sprite::draw(const vector3 vertices[4]) const
71 {
72 bind();
73
74 glVertexPointer(3, GL_SCALAR, 0, vertices[0].data());
75 glTexCoordPointer(2, GL_SCALAR, 0, tile_);
76
77 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
78 }
79
80
81 /*
82 void min_filter(GLuint filter)
83 {
84 bind();
85 mMinFilter = filter;
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
87 }
88
89 void mag_filter(GLuint filter)
90 {
91 bind();
92 mMagFilter = filter;
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
94 }
95
96 void wrap_s(GLuint wrap)
97 {
98 bind();
99 mWrapS = wrap;
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
101 }
102
103 void wrap_t(GLuint wrap)
104 {
105 bind();
106 mWrapT = wrap;
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
108 }
109
110
111 bool sprite::tile_coordinates(int index, scalar coords[8],
112 orientation orientation) const
113 {
114 if (tile_coordinates(index, coords))
115 {
116 if (orientation & flip)
117 {
118 // this looks kinda weird, but it's just swapping in a way that
119 // doesn't require an intermediate variable
120 coords[1] = coords[5];
121 coords[5] = coords[3];
122 coords[3] = coords[7];
123 coords[7] = coords[5];
124 }
125 if (orientation & reverse)
126 {
127 coords[0] = coords[2];
128 coords[2] = coords[6];
129 coords[4] = coords[6];
130 coords[6] = coords[0];
131 }
132
133 return true;
134 }
135
136 return false;
137 }
138 */
139
140
141 } // namespace moof
142
This page took 0.035916 seconds and 4 git commands to generate.