gpt4 book ai didi

C++和SDL问题

转载 作者:行者123 更新时间:2023-11-28 08:22:35 25 4
gpt4 key购买 nike

我想对我在两个类中创建的表面进行位 block 传输。一个叫做 Map,它包含相关的 map vector 以及其他一些东西。另一个是 Tile 类。运行程序时出现问题。

我没有收到任何错误,程序正常运行。有任何想法吗?这可能是某个地方的愚蠢错误。

Map populate

void map::Populate(map M)
for(int x=0;x<=19;x++)
{
for(int y=0;y<=15;y++)
{
int y2 = (y*32);
int x2 = (y*32);
Tile T(x2,y2);
M.AddToMap(&T);
printf("Added Tile");

Render

void map::Render(SDL_Surface* screen)
{
for(int x=0;x<grid.size();x++)
{
printf("test");
Tile* T = grid[x];
SDL_Surface* k = T->GetIcon();
SDL_Rect dstrect;
dstrect.x = (screen->w - k->w) / 2;
dstrect.y = (screen->h - k->h) / 2;
SDL_BlitSurface(k, 0, screen, &dstrect);

最佳答案

你并没有说明问题到底是什么,只是程序“按应有的方式运行”。

代码中的问题:

int x2 = (y*32); 应该是 x*32。

void map::Populate(map M) 按值获取 map - 这会复制您传递的 map ,任何更改都不会在传递的 map 中可见. map & M 传递一个引用,因此更改将在您传递的 map 中看到。

M.AddToMap(&T) 添加指向 local Tile 变量的指针,该变量在内循环的每次迭代中都会失效。您更有可能希望那里有 new Tile(T),或者更好的是智能指针,例如 boost 的 shared_ptr。请记住,如果您不使用智能指针,您还需要删除这些 Tiles。

新代码:

void map::Populate(map & M)
for(int x=0; x<20; x++)
{
for(int y=0; y<16; y++)
{
int y2 = (y*32);
int x2 = (x*32);
M.AddToMap(new Tile(x2,y2));
printf("Added Tile");

关于C++和SDL问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249050/

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