gpt4 book ai didi

c++ - 窗口渲染上的 SDL2.0 屏幕 nullptr

转载 作者:行者123 更新时间:2023-11-30 01:45:26 25 4
gpt4 key购买 nike

嘿,我对 SDL 库还比较陌生,只是想掌握它。

我找到了 Minecraft4k 的 C++ 转换,但它基于 SDL1.x,所以我试图将它转换为 SDL2.0

目前构建成功,但是当它到达时;

plot(x, y, rgbmul(col, fxmul(br, ddist)));

它抛出读取访问冲突异常:

screen was nullptr

这是我的代码;

// C++ port of Minecraft 4k JS (http://jsdo.it/notch/dB1E)
// By The8BitPimp
// See: the8bitpimp.wordpress.com

#include <SDL.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>
#include "plot.h"
#include "llist.h"

const int w = 320;
const int h = 240;

SDL_Surface *screen = nullptr;

const float math_pi = 3.14159265359f;

static inline float math_sin(float x) {
return sinf(x);
}

static inline float math_cos(float x) {
return cosf(x);
}

// the texture map
int texmap[16 * 16 * 16 * 3];

// the voxel map
char map[64 * 64 * 64];

static inline int random(int max) {
return (rand() ^ (rand() << 16)) % max;
}

static inline void plot(int x, int y, int c) {
int *p = (int*)screen->pixels;
p[y * w + x] = c;
}

static void makeTextures(void) {

// each texture
for (int j = 0; j<16; j++) {

int k = 255 - random(96);

// each pixel in the texture
for (int m = 0; m<16 * 3; m++)
for (int n = 0; n<16; n++) {

int i1 = 0x966C4A;
int i2 = 0;
int i3 = 0;

if (j == 4)
i1 = 0x7F7F7F;
if ((j != 4) || (random(3) == 0))
k = 255 - random(96);
if (j == 1)
{
if (m < (((n * n * 3 + n * 81) >> 2) & 0x3) + 18)
i1 = 0x6AAA40;
else if (m < (((n * n * 3 + n * 81) >> 2) & 0x3) + 19)
k = k * 2 / 3;
}
if (j == 7)
{
i1 = 0x675231;
if ((n > 0) && (n < 15) && (((m > 0) && (m < 15)) || ((m > 32) && (m < 47))))
{
i1 = 0xBC9862;
i2 = n - 7;
i3 = (m & 0xF) - 7;
if (i2 < 0)
i2 = 1 - i2;

if (i3 < 0)
i3 = 1 - i3;

if (i3 > i2)
i2 = i3;

k = 196 - random(32) + i2 % 3 * 32;
}
else if (random(2) == 0)
k = k * (150 - (n & 0x1) * 100) / 100;
}
if (j == 5)
{
i1 = 0xB53A15;
if (((n + m / 4 * 4) % 8 == 0) || (m % 4 == 0))
i1 = 0xBCAFA5;
}
i2 = k;
if (m >= 32)
i2 /= 2;
if (j == 8)
{
i1 = 5298487;
if (random(2) == 0)
{
i1 = 0;
i2 = 255;
}
}

// fixed point colour multiply between i1 and i2
i3 =
((((i1 >> 16) & 0xFF) * i2 / 255) << 16) |
((((i1 >> 8) & 0xFF) * i2 / 255) << 8) |
((i1 & 0xFF) * i2 / 255);
// pack the colour away
texmap[n + m * 16 + j * 256 * 3] = i3;
}
}
}

static void makeMap(void) {
// add random blocks to the map
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
for (int z = 0; z < 64; z++) {
int i = (z << 12) | (y << 6) | x;
float yd = (y - 32.5) * 0.4;
float zd = (z - 32.5) * 0.4;
map[i] = random(16);

float th = random(256) / 256.0f;

if (th > sqrtf(sqrtf(yd * yd + zd * zd)) - 0.8f)
map[i] = 0;
}
}
}
}

static void init(void) {
makeTextures();
makeMap();
}

// fixed point byte byte multiply
static inline int fxmul(int a, int b) {
return (a*b) >> 8;
}

// fixed point 8bit packed colour multiply
static inline int rgbmul(int a, int b) {
int _r = (((a >> 16) & 0xff) * b) >> 8;
int _g = (((a >> 8) & 0xff) * b) >> 8;
int _b = (((a)& 0xff) * b) >> 8;
return (_r << 16) | (_g << 8) | _b;
}

static void render(void) {

float now = (float)(SDL_GetTicks() % 10000) / 10000.f;

float xRot = math_sin(now * math_pi * 2) * 0.4 + math_pi / 2;
float yRot = math_cos(now * math_pi * 2) * 0.4;
float yCos = math_cos(yRot);
float ySin = math_sin(yRot);
float xCos = math_cos(xRot);
float xSin = math_sin(xRot);

float ox = 32.5 + now * 64.0;
float oy = 32.5;
float oz = 32.5;

// for each column
for (int x = 0; x < w; x++) {
// get the x axis delta
float ___xd = ((float)x - (float)w / 2.f) / (float)h;
// for each row
for (int y = 0; y < h; y++) {
// get the y axis delta
float __yd = ((float)y - (float)h / 2.f) / (float)h;
float __zd = 1;
float ___zd = __zd * yCos + __yd * ySin;
float _yd = __yd * yCos - __zd * ySin;
float _xd = ___xd * xCos + ___zd * xSin;
float _zd = ___zd * xCos - ___xd * xSin;

int col = 0;
int br = 255;
float ddist = 0;

float closest = 32.f;

// for each principle axis x,y,z
for (int d = 0; d < 3; d++) {
float dimLength = _xd;
if (d == 1)
dimLength = _yd;
if (d == 2)
dimLength = _zd;

float ll = 1.0f / (dimLength < 0.f ? -dimLength : dimLength);
float xd = (_xd)* ll;
float yd = (_yd)* ll;
float zd = (_zd)* ll;

float initial = ox - floor(ox);
if (d == 1) initial = oy - floor(oy);
if (d == 2) initial = oz - floor(oz);

if (dimLength > 0) initial = 1 - initial;

float dist = ll * initial;

float xp = ox + xd * initial;
float yp = oy + yd * initial;
float zp = oz + zd * initial;

if (dimLength < 0) {
if (d == 0) xp--;
if (d == 1) yp--;
if (d == 2) zp--;
}

// while we are concidering a ray that is still closer then the best so far
while (dist < closest) {

// quantize to the map grid
int tex = map[(((int)zp & 63) << 12) | (((int)yp & 63) << 6) | ((int)xp & 63)];

// if this voxel has a texture applied
if (tex > 0) {

// find the uv coordinates of the intersection point
int u = ((int)((xp + zp) * 16.f)) & 15;
int v = ((int)(yp * 16.f) & 15) + 16;

// fix uvs for alternate directions?
if (d == 1) {
u = ((int)(xp * 16.f)) & 15;
v = (((int)(zp * 16.f)) & 15);
if (yd < 0)
v += 32;
}

// find the colour at the intersection point
int cc = texmap[u + v * 16 + tex * 256 * 3];

// if the colour is not transparent
if (cc > 0) {
col = cc;
ddist = 255 - ((dist / 32 * 255));
br = 255 * (255 - ((d + 2) % 3) * 50) / 255;

// we now have the closest hit point (also terminates this ray)
closest = dist;
}
}

// advance the ray
xp += xd;
yp += yd;
zp += zd;
dist += ll;
}
}

plot(x, y, rgbmul(col, fxmul(br, ddist)));
}
}
}

int main(int argc, char *argv[]) {

SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *screen;
screen = SDL_CreateWindow(
"Minecraft4k", // window title
SDL_WINDOWPOS_CENTERED, // initial x position
SDL_WINDOWPOS_CENTERED, // initial y position
320, // width, in pixels
240, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);

SDL_Renderer* renderer;
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);

if (screen == nullptr) {
return 1;
}

init();
bool running = true;
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
running &= (event.type != SDL_QUIT);
}
SDL_RenderPresent(renderer);
render();
}

SDL_DestroyWindow(screen);
SDL_Quit();
return 0;

}

当我实际运行代码时,我确实遇到了黑屏,但调试器在线上着陆了

plot(x, y, rgbmul(col, fxmul(br, ddist)));

在; 静态无效渲染(无效)

这一切都只是为了“好玩”,因此欢迎任何信息或指导。

最佳答案

您定义了 screen 两次(第一次作为全局变量,第二次在您的 main 中),但您只初始化了一次(在您的 中)主).
因此,全局变量 screen 实际上设置为 nullptr 并且 plot 尝试使用它失败,如错误消息所述。

关于c++ - 窗口渲染上的 SDL2.0 屏幕 nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com