/
graph2.h
178 lines (150 loc) · 4.31 KB
/
graph2.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// a libsdl/libgd wrapper
// Copyright (C) 2018 Zeno Rogue, see 'nconf.cpp' for details
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <cstdio>
#include <cmath>
#include <string>
#if CAP_BMP
#include <SDL/SDL.h>
#endif
#if CAP_GD
#include <gd.h>
#endif
namespace graph2 {
// ipoint
struct ipoint {
int x, y;
ipoint(int _x, int _y): x(_x), y(_y) {}
ipoint() {}
ipoint& operator += (const ipoint& a) { x += a.x; y += a.y; return *this; }
ipoint& operator -= (const ipoint& a) { x -= a.x; y -= a.y; return *this; }
ipoint operator + (ipoint a) const { a += (*this); return a; }
ipoint operator - (ipoint a) const { ipoint p = (*this); p -= a; return p; }
bool operator == (const ipoint b) { return x == b.x && y == b.y; }
};
typedef long double ld;
// not necessary in C++17
// template<class T> int size(const T& t) { return t.size(); }
typedef unsigned int color;
typedef unsigned char colorpart;
colorpart& part(color& col, int i) {
colorpart* c = (colorpart*) &col;
return c[i];
}
void die(const std::string& s) {
fprintf(stderr, "%s\n", s.c_str());
exit(1);
}
void pdie(const std::string& s) {
perror(s.c_str());
exit(1);
}
#if CAP_BMP
color errpixel;
struct pixelrow {
color *v;
int size;
color& operator [] (int x) { if(x >= size || x < 0) return errpixel; else return v[x]; }
};
struct bitmap {
SDL_Surface *s;
bool locked;
bool isscreen;
void draw() {
if(isscreen) beunlocked(), SDL_UpdateRect(s, 0, 0, 0, 0);
}
void belocked() {
if(isscreen && !locked) locked = true, SDL_LockSurface(s);
}
void beunlocked() {
if(isscreen && locked) locked = false, SDL_UnlockSurface(s);
}
color& operator [] (ipoint xy) const { return (*this)[xy.y][xy.x]; }
pixelrow operator [] (int y) const {
if(y<0 || y>=s->h) return pixelrow {&errpixel, 1};
unsigned char *dst = (unsigned char*) s->pixels;
dst += y*s->pitch;
color* ptr = (color*) dst;
return pixelrow{ptr, s->w};
}
~bitmap() { if(s && !isscreen) SDL_FreeSurface(s); }
bitmap& operator= (bitmap&& b) { s = b.s; locked = b.locked; isscreen = b.isscreen; b.s = NULL; return *this; }
bitmap(bitmap&& b) { s = b.s; locked = b.locked; isscreen = b.isscreen; b.s = NULL; }
bitmap(SDL_Surface *s, bool l, bool is) : s(s), locked(l), isscreen(is) {}
bitmap() { s = NULL; }
};
bitmap screen ( NULL, false, true );
bitmap surfaceToBitmap(SDL_Surface *s) {
bitmap b;
b.s = s; b.locked = true; b.isscreen = false;
return b;
}
void initGraph(int sx, int sy, const std::string& title, int flags) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL error\n");
SDL_Quit();
}
if(screen.s && sx == screen.s->w && sy == screen.s->h) return;
screen.s = SDL_SetVideoMode(sx, sy, 32, flags);
SDL_WM_SetCaption(title.c_str(), 0);
SDL_EnableKeyRepeat(500, 10);
SDL_EnableUNICODE(1);
}
SDL_Surface *emptySurface(int sx, int sy) {
return SDL_CreateRGBSurface(SDL_SWSURFACE, sx, sy, 32, 0xFF<<16,0xFF<<8,0xFF,0xFF<<24);
}
bitmap emptyBitmap(int sx, int sy) {
return bitmap(emptySurface(sx, sy), true, false);
}
#if CAP_GD
bitmap fromGD(gdImagePtr im) {
bitmap tgt = emptyBitmap(im->sx, im->sy);
for(int y=0; y<im->sy; y++)
for(int x=0; x<im->sx; x++) {
if(im->trueColor)
tgt[y][x] = im->tpixels[y][x];
else {
int p = im->pixels[y][x];
color& px(tgt[y][x]);
part(px, 2) = im->red[p];
part(px, 1) = im->green[p];
part(px, 0) = im->blue[p];
part(px, 3) = 255 - im->alpha[p];
}
part(tgt[y][x], 3) = 255 - part(tgt[y][x], 3);
}
return tgt;
}
gdImagePtr toGD(const bitmap &bmp) {
gdImagePtr im = gdImageCreateTrueColor(bmp.s->w, bmp.s->h);
for(int y=0; y<im->sy; y++)
for(int x=0; x<im->sx; x++) {
color c = bmp[y][x];
part(c, 3) = 255 - part(c, 3);
im->tpixels[y][x] = c;
}
return im;
}
bitmap readPng(const std::string& fname) {
FILE *f = fopen(fname.c_str(), "rb");
if(!f) pdie("readPng");
gdImagePtr im = gdImageCreateFromPng(f);
bitmap tgt = fromGD(im);
gdImageDestroy(im);
fclose(f);
return tgt;
}
void writePng(const std::string& fname, const bitmap& bmp) {
FILE *f = fopen(fname.c_str(), "wb");
if(!f) pdie("writePng");
gdImagePtr im = toGD(bmp);
gdImageSaveAlpha(im, 1);
gdImagePng(im, f);
gdImageDestroy(im);
fclose(f);
}
#endif
#endif
}
#endif