forked from vxmdesign/sc2kviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScDisplay.cpp
138 lines (126 loc) · 3.15 KB
/
ScDisplay.cpp
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
133
134
135
136
137
138
#include "ScDisplay.h"
ScDisplay::ScDisplay(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
printf("Could not init!\n");
}
// mPalette = new Sc2kPalette("sc2k.pal");
mPalette = new Sc2kPalette();
}
ScDisplay::~ScDisplay(){
SDL_FreeSurface(mDisplay);
SDL_FreeSurface(mImage);
SDL_Quit();
}
int ScDisplay::initGraph(int pWidth, int pHeight){
int c;
unsigned int *ptr;
mWidth = pWidth;
mHeight = pHeight;
if((mDisplay = SDL_SetVideoMode(pWidth, pHeight, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {
printf("Could not make display buffer\n");
return -1;
}
if((mImage = SDL_CreateRGBSurface(SDL_SWSURFACE, pHeight, pWidth, 32,0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000))== NULL){
ptr = (unsigned int*) mImage->pixels;
for(c = 0; c < (mWidth * mHeight); c++){
ptr[c] = 0x00FFFFFF;
}
printf("Could not make image buffer\n");
return -1;
}
// SDL_SetColorKey(mImage, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(mImage->format, 255, 1, 255));
return 0;
}
void ScDisplay::setPixel(int pX, int pY, unsigned int pColor){
unsigned int *px;
int idx;
px =(unsigned int*)mImage->pixels;
idx = (pY * mWidth) + pX;
px[idx] = pColor;
}
void ScDisplay::display(ShapHeader *pSh){
SDL_Rect dst;
SDL_Surface *stmp;
int c;
unsigned int *ptr;
ptr = (unsigned int*) mImage->pixels;
for(c = 0; c < (mWidth * mHeight); c++){
ptr[c] = 0x00FFFFFF;
}
process(pSh);
dst.x = 0;
dst.y = 0;
stmp = SDL_DisplayFormat(mImage);
SDL_BlitSurface(stmp, NULL, mDisplay, &dst);
SDL_FreeSurface(stmp);
SDL_Flip(mDisplay);
}
int ScDisplay::process(ShapHeader *pSh){
unsigned char *pl;
int idx;
int linenum;
int llen;
int xpos;
int off;
int pixels;
int px;
pl = &(pSh->index[18]);
idx = 0;
for(linenum = 0; linenum < pSh->height; linenum++){
xpos = 0;
if(pl[idx+1] != 0x01){
if(pl[idx+1] == 0x2 && pl[idx] == 0){
printf("WEIRD\n");
}else{
printf("line increment expected!\n");
return -1;
}
}
llen = pl[idx];
idx+=2;
off = 0;
while(off < llen){
switch(pl[idx + off + 1]){
case 2:
if(linenum != pSh->height - 1){
printf("Expected end 2 arg at end of frame %d %d\n", linenum, pSh->height);
return -1;
}
if(pl[idx+off] != 2){
printf("Though end of frame arg should be 2: %02x\n", pl[idx+off]);
}
off+=2;
break;
case 3:
xpos += pl[idx + off];
off+=2;
break;
case 4:
pixels = pl[idx+off];
off+=2;
for(px = 0; px < pixels; px++){
setPixel(xpos, linenum, mPalette->mapPixel(pl[idx+off]));
xpos++;
off++;
}
if((pixels & 0x1) != 0){
if(pl[idx+off] != 0){
printf("I this should be some extra zero %02x %d\n", pl[idx+off], linenum);
// return -1;
}
off++;
}
break;
case 0:
///why is this here?
off+=2;
break;
default:
printf("strange command %02x %d %d %d\n", pl[idx+off+1], idx, off, linenum);
return -1;
}
}
idx+=off;
}
return 0;
}