forked from AppleWin/AppleWin
-
Notifications
You must be signed in to change notification settings - Fork 11
/
retroframe.cpp
182 lines (150 loc) · 4.62 KB
/
retroframe.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
#include "StdAfx.h"
#include "frontends/libretro/retroframe.h"
#include "frontends/libretro/environment.h"
#include "Interface.h"
#include "Core.h"
#include "Utilities.h"
#include <fstream>
namespace
{
void readFileToBuffer(const std::string & filename, std::vector<char> & buffer)
{
std::ifstream file(filename.c_str(), std::ios::binary | std::ios::ate);
const std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
buffer.resize(size);
file.read(buffer.data(), size);
}
template<typename T>
T getAs(const std::vector<char> & buffer, const size_t offset)
{
if (offset + sizeof(T) > buffer.size())
{
throw std::runtime_error("Invalid bitmap");
}
const T * ptr = reinterpret_cast<const T *>(buffer.data() + offset);
return * ptr;
}
// libretro cannot parse BMP with 1 bpp
// simple version implemented here
bool getBitmapData(const std::vector<char> & buffer, int32_t & width, int32_t & height, uint16_t & bpp, const char * & data, uint32_t & size)
{
if (buffer.size() < 2)
{
return false;
}
if (buffer[0] != 0x42 || buffer[1] != 0x4D)
{
return false;
}
const uint32_t fileSize = getAs<uint32_t>(buffer, 2);
if (fileSize != buffer.size())
{
return false;
}
const uint32_t offset = getAs<uint32_t>(buffer, 10);
const uint32_t header = getAs<uint32_t>(buffer, 14);
if (header != 40)
{
return false;
}
width = getAs<int32_t>(buffer, 18);
height = getAs<int32_t>(buffer, 22);
bpp = getAs<uint16_t>(buffer, 28);
const uint32_t imageSize = getAs<uint32_t>(buffer, 34);
if (offset + imageSize > fileSize)
{
return false;
}
data = buffer.data() + offset;
size = imageSize;
return true;
}
}
namespace ra2
{
RetroFrame::RetroFrame()
{
}
void RetroFrame::FrameRefreshStatus(int drawflags)
{
if (drawflags & DRAW_TITLE)
{
GetAppleWindowTitle();
display_message(g_pAppTitle.c_str());
}
}
void RetroFrame::VideoPresentScreen()
{
// this should not be necessary
// either libretro handles it
// or we should change AW
// but for now, there is no alternative
for (size_t row = 0; row < myHeight; ++row)
{
const uint8_t * src = myFrameBuffer + row * myPitch;
uint8_t * dst = myVideoBuffer.data() + (myHeight - row - 1) * myPitch;
memcpy(dst, src, myPitch);
}
video_cb(myVideoBuffer.data() + myOffset, myBorderlessWidth, myBorderlessHeight, myPitch);
}
void RetroFrame::Initialize(bool resetVideoState)
{
CommonFrame::Initialize(resetVideoState);
FrameRefreshStatus(DRAW_TITLE);
Video & video = GetVideo();
myBorderlessWidth = video.GetFrameBufferBorderlessWidth();
myBorderlessHeight = video.GetFrameBufferBorderlessHeight();
const size_t borderWidth = video.GetFrameBufferBorderWidth();
const size_t borderHeight = video.GetFrameBufferBorderHeight();
const size_t width = video.GetFrameBufferWidth();
myHeight = video.GetFrameBufferHeight();
myFrameBuffer = video.GetFrameBuffer();
myPitch = width * sizeof(bgra_t);
myOffset = (width * borderHeight + borderWidth) * sizeof(bgra_t);
const size_t size = myHeight * myPitch;
myVideoBuffer.resize(size);
}
void RetroFrame::Destroy()
{
CommonFrame::Destroy();
myFrameBuffer = nullptr;
myVideoBuffer.clear();
}
void RetroFrame::GetBitmap(LPCSTR lpBitmapName, LONG cb, LPVOID lpvBits)
{
const std::string filename = getBitmapFilename(lpBitmapName);
const std::string path = getResourcePath(filename);
std::vector<char> buffer;
readFileToBuffer(path, buffer);
if (!buffer.empty())
{
int32_t width, height;
uint16_t bpp;
const char * data;
uint32_t size;
const bool res = getBitmapData(buffer, width, height, bpp, data, size);
log_cb(RETRO_LOG_INFO, "RA2: %s. %s = %dx%d, %dbpp\n", __FUNCTION__, path.c_str(),
width, height, bpp);
if (res && height > 0 && size <= cb)
{
const size_t length = size / height;
// rows are stored upside down
char * out = static_cast<char *>(lpvBits);
for (size_t row = 0; row < height; ++row)
{
const char * src = data + row * length;
char * dst = out + (height - row - 1) * length;
memcpy(dst, src, length);
}
return;
}
}
CommonFrame::GetBitmap(lpBitmapName, cb, lpvBits);
}
int RetroFrame::FrameMessageBox(LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
log_cb(RETRO_LOG_INFO, "RA2: %s: %s - %s\n", __FUNCTION__, lpCaption, lpText);
return IDOK;
}
}