-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFX_SDL_1.2.c
More file actions
187 lines (167 loc) · 5.11 KB
/
GFX_SDL_1.2.c
File metadata and controls
187 lines (167 loc) · 5.11 KB
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
/*
SDL 1.2 Graphics Backend
Also handles other useful functions (such as the 10ms music timer)
This file is part of Rhombus: the emulator for LFT's FPGA demo "Parallelogram".
Copyright (C) 2015 sirwinstoncat5 (GitHub username)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "SDL/SDL.h"
extern uint16_t outPort[8];
SDL_Surface* qvga_buffer=NULL;
SDL_Surface* screen=NULL;
void RhombusGFXInit(int scr_height, int scr_width);
void RhombusGFXQuit(void);
void RhombusDrawFrame(void);
void putpixel(SDL_Surface *surface, int x, int y, uint32_t pixel);
uint32_t getpixel(SDL_Surface *surface, int x, int y);
void scaleBlit(SDL_Surface* src, SDL_Surface* dst);
void RhombusGFXInit(int scr_width, int scr_height)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Rhombus: the Parallelogram by LFT FPGA Demo Emulator", "Rhombus");
SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL);
freopen( "CON", "w", stdout ); //Redirect stdout back to console
freopen( "CON", "w", stderr );
screen=SDL_SetVideoMode(scr_width, scr_height, 32, SDL_SWSURFACE);
SDL_Surface* splash=SDL_LoadBMP("hello.bmp");
scaleBlit(splash, screen);
SDL_Flip(screen);
SDL_FreeSurface(splash);
qvga_buffer=SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, 32, 0, 0, 0, 0);
}
void RhombusGFXQuit(void)
{
SDL_FreeSurface(qvga_buffer);
SDL_Quit();
}
void RhombusDrawFrame(void)
{
SDL_Event evt;
SDL_PollEvent(&evt);
if(evt.type==SDL_QUIT) exit(0);
SDL_LockSurface(qvga_buffer);
int addr;
addr=131072*outPort[6];
int i, j, k;
uint32_t pix;
//Begin test routine
k=0;
for(i=0; i<320; i++) for(j=0; j<240; j++)
{
//if(rand()<(RAND_MAX/2)) pix=1;
//else pix=0-1;
//putpixel(qvga_buffer, i, j, 0b00000000000000000000001111111111); //Draw a test screen
// Generated blue
//putpixel(qvga_buffer, i, j, 0b00000000000011111111110000000000); //Draw a test screen
// Generated green
//putpixel(qvga_buffer, i, j, 0b11111111110000000000000000000000); //Draw a test screen
// Generated red
if(k>0b00000000000000000000001111111111) k=0;
putpixel(qvga_buffer, i, j, k++);
}
//End test routine
//Insert actual framebuffer drawing here
SDL_UnlockSurface(qvga_buffer);
scaleBlit(qvga_buffer, screen);
SDL_Flip(screen);
}
void scaleBlit(SDL_Surface* src, SDL_Surface* dst) //Should have used SDL 2, probably a huge bottleneck, oh well.
{
SDL_LockSurface(src);
SDL_LockSurface(dst);
float i, j;
float scalex=(float) (src->w)/(float) (dst->w);
float scaley=(float) (src->h)/(float) (dst->h);
//printf("scale: %f, %f\n", scalex, scaley);
//getchar();
for(i=0; i<dst->w; i++)
{
for(j=0; j<dst->h; j++)
{
//printf("xy to xy: %d, %d, %d, %d\n", (int) (i*scalex), (int) (j*scaley), (int) i, (int) j);
putpixel(dst, i, j, getpixel(src, (int) (i*scalex), (int) (j*scaley)));
}
}
SDL_UnlockSurface(src);
SDL_UnlockSurface(dst);
}
//Copied from SDL spec:
//void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
//Uint32 getpixel(SDL_Surface *surface, int x, int y)
//Before use, lock surface using SDL_LockSurface(surface)
//Unlock using SDL_UnlockSurface
/*
SDL's coordinates are like....
+----------------------->
|0.......................x
|.
|.
|.
|.
|.
|.
|.
|.
|.
|y
\/
*/
void putpixel(SDL_Surface *surface, int x, int y, uint32_t pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
uint32_t getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}