-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdgrenderer.cpp
189 lines (168 loc) · 4.42 KB
/
cdgrenderer.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
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
179
180
181
182
183
184
185
186
187
188
189
#include "stdafx.h"
#include "cdg.h"
#include "cdgrenderer.h"
#include "png.h"
void cdg_render(const CDG &cdg, SDL_Renderer *renderer, bool maskBorder, int scaling)
{
uint8_t pv, ph;
uint8_t r, g, b, a;
uint8_t border;
cdg.getPointers(pv, ph);
border = cdg.getBorderColor();
if (scaling == 1)
{
//optimize for the case of scaling = 1
for (int x = 0; x + ph < CDG_WIDTH; x++)
{
for (int y = 0; y + pv < CDG_HEIGHT; y++)
{
cdg.getColor(cdg.getPixel(x + ph, y + pv), r, g, b, a);
SDL_SetRenderDrawColor(renderer, r, g, b, ~a);
SDL_RenderDrawPoint(renderer, x, y);
}
}
}
else
{
for (int x = 0; x + ph < CDG_WIDTH; x++)
{
for (int y = 0; y + pv < CDG_HEIGHT; y++)
{
cdg.getColor(cdg.getPixel(x + ph, y + pv), r, g, b, a);
SDL_SetRenderDrawColor(renderer, r, g, b, ~a);
SDL_Rect pixelRect;
pixelRect.x = x * scaling;
pixelRect.y = y * scaling;
pixelRect.w = scaling;
pixelRect.h = scaling;
SDL_RenderFillRect(renderer, &pixelRect);
}
}
}
if (maskBorder)
{
cdg.getColor(border, r, g, b, a);
SDL_SetRenderDrawColor(renderer, r, g, b, ~a);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = CDG_WIDTH * scaling;
rect.h = ROW_MULT * scaling;
SDL_RenderFillRect(renderer, &rect);
rect.w = COL_MULT * scaling;
rect.h = CDG_HEIGHT * scaling;
SDL_RenderFillRect(renderer, &rect);
rect.x = (CDG_WIDTH - COL_MULT) * scaling;
SDL_RenderFillRect(renderer, &rect);
rect.x = 0;
rect.y = (CDG_HEIGHT - ROW_MULT) * scaling;
rect.w = CDG_WIDTH * scaling;
rect.h = ROW_MULT * scaling;
SDL_RenderFillRect(renderer, &rect);
}
}
//http://www.lemoda.net/c/write-png/
//what a mess
bool save_cdg_screen(const CDG &cdg, bool maskBorder, const char *file)
{
uint8_t pv, ph;
uint8_t r, g, b, a;
uint8_t border;
cdg.getPointers(pv, ph);
border = cdg.getBorderColor();
bool status = false;
FILE * fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
int pixel_size = 4; //rgba
#ifdef WIN32
bool fileIsOpen = !fopen_s(&fp, file, "wb");
#else
fp = fopen(file, "wb");
bool fileIsOpen = !fp;
#endif
if (file && fileIsOpen)
{
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr)
{
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr)
{
// Set up error handling.
// what is this even?????????
if (!setjmp(png_jmpbuf (png_ptr)))
{
//Set image attributes.
png_set_IHDR(png_ptr,
info_ptr,
CDG_WIDTH,
CDG_HEIGHT,
8, //bpp depth
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
// Initialize rows of PNG.
//int i = 255;
row_pointers = (png_byte**) png_malloc(png_ptr, CDG_HEIGHT * sizeof(png_byte*));
for (y = 0; y < CDG_HEIGHT; ++y)
{
png_byte *row = (png_byte*) png_malloc(png_ptr, CDG_WIDTH * pixel_size);
row_pointers[y] = row;
for (x = 0; x < CDG_WIDTH; ++x)
{
if (maskBorder && (x < COL_MULT
|| x > CDG_WIDTH - COL_MULT
|| y < ROW_MULT
|| y > CDG_HEIGHT - ROW_MULT))
cdg.getColor(border, r, g, b, a);
else if (x + ph < CDG_WIDTH
&& y + pv < CDG_HEIGHT)
cdg.getColor(cdg.getPixel(x + ph, y + pv), r, g, b, a);
else
r = g = b = a = 0;
*row++ = r;
*row++ = g;
*row++ = b;
*row++ = (~a);
/*
//cool pattern this
*row++ = x % 255; //r
*row++ = y % 255; //g
*row++ = (x * y) %255; //b
*row++ = i--; //a
if (i == 0)
i = 255;*/
}
/*for (x = 0; x < CDG_WIDTH; ++x) {
pixel_t * pixel = pixel_at (bitmap, x, y);
*row++ = pixel->red;
*row++ = pixel->green;
*row++ = pixel->blue;
}*/
}
// Write the image data to "fp".
png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
for (y = 0; y < CDG_HEIGHT; y++)
{
//something crashes around here
png_free(png_ptr, row_pointers[y]);
}
png_free(png_ptr, row_pointers);
status = true;
}
// I'm not sure if the png_destroy cleanup codes lines up
//with what I have above
png_destroy_info_struct(png_ptr, &info_ptr);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
}
fclose(fp);
}
return status;
}