summaryrefslogtreecommitdiff
path: root/lib/sprite.cc
blob: be2fd3b7542528c3c32e9bd71fda4bc8bb50dbff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <SDL.h>
#include "sprite.h"

#define TEXSIZE 256

mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0;

mogltk::Sprite::Sprite(Handle * h, int asx, int asy) : sx(asx), sy(asy) {
    alloc();
    for (int y = 0; y < sy; y++) {
	h->read(tlist->GetTex()->pixels + TEXSIZE * y + posx, sx);
    }
}

mogltk::Sprite::Sprite(Uint8 * p, int asx, int asy) : sx(asx), sy(asy) {
    alloc();
    for (int y = 0; y < sy; y++) {
	bcopy(p, tlist->GetTex()->pixels + TEXSIZE * y + posx, sx);
	p += sx;
    }
}

mogltk::Sprite::~Sprite() {
    if (prev)
	prev->next = next
    else
	tlist->sprheader = next;
    if (next)
	next->prev = prev;

    if (!tlist->sprheader) {
	delete tlist;
    }
}

void mogltk::Sprite::alloc() {
    /* FIXMEEEEEEEEEEEEE */
    bool found = false;
    TexList * p;

    for (p = tlist; p && !found; p = p->GetNext()) {
	for (y = 0; (py < (TEXSIZE - sy)) && !found; py++) {
	    for (x = 0; (px < (TEXSIZE - sx)) && !found; px++) {
		if (p->strheader->canfit(x, y, x + sx, y + sy)) {
		    found = true;
		}
	    }
	}
    }
    
    if (!found) {
#ifdef DEBUG
	printm(M_INFO, "Allocating a new texture for sprites\n");
#endif
	x = y = 0;
	p = new TexList(TEXSIZE);
    }
    
    tlist = p;
}

mogltk::Sprite::TexList::TexList(int size) {
    tex = new texture(size, size, true);

    if (header)
	header->next->prev = this;
    prev = 0;
    next = header;
    header = this;    
}

mogltk::Sprite::TexList::~TexList() {
    delete tex;
    if (prev)
	prev->next = next
    else
	header = next;
    if (next)
	next->prev = prev;
}

const mogltk::texture * mogltk::Sprite::TexList::GetTex() const {
    return tex;
}

const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() const {
    return header;
}

const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() const {
    return next;
}

void mogltk::Sprite::TexList::Bind() const {
    tex->Bind();
}

bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const {
    int sx1 = x, sy1 = y, sx2 = x + sx, sy2 = y + sy;
    
    return !((sx1 >= x2) || (sx2 <= x1) || (sy1 >= y1) || (sy2 <= y2));
}

bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const {
    if (!intersect(x1, y1, x2, y2))
	return true
    else
	if (next)
	    return next->canfit()
	else
	    return false;
}

void mogltk::Sprite::draw(int dx, int dy, Color c) {
    bool was2D;
    
    was2D = mogltk::glbase::is2D();
    
    if (!was2D)
	mogltk::glbase::Enter2DMode();

    tlist->Bind();
    glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2i(x         , y         ); glVertex2i(dx         , dy         );
    glTexCoord2i(x + sx - 1, y         ); glVertex2i(dx + sx - 1, dy         );
    glTexCoord2i(x         , y + sy - 1); glVertex2i(dx         , dy + sy - 1);
    glTexCoord2i(x + sx - 1, y + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1);
    glEnd();

    if (!was2D)
	mogltk::glbase::Leave2DMode();
}