- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的很熟悉使用SDL进行c++编程,并且在运行程序时遇到了一个非常奇怪的问题:当SDL到达SDL_CreateTextureFromSurface时,SDL崩溃,然后我什至无法使用任务管理器关闭窗口!我很困惑,因为代码运行得很好,并且在出现此问题之前我没有做任何更改!甚至尝试使用调试器,但未能找到错误:
经过数小时的谷歌搜索,没有发现任何令人满意的结果。这是我的代码:
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <vector>
const int WIN_W = 1000;
const int WIN_H = 550;
const int CEN_W = 511;
const int CEN_H = 511;
const int CEN_X = (WIN_W - CEN_W) / 2;
const int CEN_Y = (WIN_H - CEN_H) / 2;
const int SEP_W = 3;
const int MAX_X = 4;
const int MAX_Y = 4;
const int nbImages = 11;
int numbers[MAX_X][MAX_Y];
int winNumbers[MAX_X][MAX_Y];
TTF_Font* gameNbFont = NULL;
TTF_Font* textFont = NULL;
TTF_Font* pixelFont = NULL;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* numbersTextures[MAX_X * MAX_Y - 1];
SDL_Texture* bgImage = NULL;
SDL_Texture* bgImages[nbImages];
SDL_Rect* numbersContsRectsPos[MAX_X][MAX_Y];
SDL_Rect* numbersContsRectsCrops[MAX_X * MAX_Y - 1];
SDL_Rect* restartButtonRect = NULL;
SDL_Texture* timeStringTexture = NULL;
SDL_Texture* timeIntTexture = NULL;
SDL_Texture* movesStringTexture = NULL;
SDL_Texture* movesIntTexture = NULL;
SDL_Texture* restartButtonTexture;
enum texturesSizesEnum{
TIME_STRING,
TIME_INT,
MOVES_STRING,
MOVES_INT,
TEXTURES_SIZES_TOTAL
};
std::pair<int, int> texturesSizes[TEXTURES_SIZES_TOTAL];
std::pair<int, int> numbersTexturesSizes[MAX_X * MAX_Y - 1];
int currentTime = -1;
int nbMoves = -1;
int timeFraction = 0;
bool stop;
void updateTime(){
if (currentTime == 999){
return;
}
currentTime ++;
std::ostringstream sm;
sm << currentTime;
std::string time = std::string(3-sm.str().length(), '0') + sm.str();
SDL_Surface* timeIntSurface = TTF_RenderText_Solid(pixelFont, time.c_str(), SDL_Color({0XFF, 0XFF, 0XFF}));
timeIntTexture = SDL_CreateTextureFromSurface(renderer, timeIntSurface);
texturesSizes[TIME_INT] = std::make_pair(timeIntSurface->w, timeIntSurface->h);
}
void updateMoves(){
if (nbMoves == 999){
return;
}
nbMoves ++;
std::ostringstream sm;
sm << nbMoves;
std::string moves = std::string(3-sm.str().length(), '0') + sm.str();
SDL_Surface* movesIntSurface = TTF_RenderText_Solid(pixelFont, moves.c_str(), SDL_Color({0XFF, 0XFF, 0XFF}));
movesIntTexture = SDL_CreateTextureFromSurface(renderer, movesIntSurface);
texturesSizes[MOVES_INT] = std::make_pair(movesIntSurface->w, movesIntSurface->h);
}
bool initTextures(){
printf("started\n");
bool success=false;
gameNbFont = TTF_OpenFont("data/fonts/gothic.ttf", 40);
if (gameNbFont == NULL){
printf("Couldn't load data/fonts/gothic.ttf font! TTF_Error: %s\n", TTF_GetError());
}
else{
printf("breakpoint1\n");
for (int i=1; i<MAX_X * MAX_Y; i++){
printf("breakpoint1.%i.1\n", i);
std::ostringstream stm;
printf("breakpoint1.%i.2\n", i);
stm << i;
printf("breakpoint1.%i.3\n", i);
SDL_Surface* number = TTF_RenderText_Solid(gameNbFont, stm.str().c_str(), SDL_Color({0X4E, 0XCD, 0XC4}));
printf("breakpoint1.%i.4\n", i); //program crashes after printing this :(
numbersTextures[i-1] = SDL_CreateTextureFromSurface(renderer, number);
printf("breakpoint1.%i.5\n", i);
SDL_FreeSurface(number);
printf("breakpoint1.%i.6\n", i);
numbersTexturesSizes[i-1] = std::make_pair(number->w, number->h);
}
printf("breakpoint2\n");
textFont = TTF_OpenFont("data/fonts/PlayfairDisplay.ttf", 50);
if (textFont == NULL){
printf("Couldn't load data/fonts/PlayfairDisplay.ttf font! TTF_Error: %s", TTF_GetError());
}
else{
printf("breakpoint3");
SDL_Surface* timeStringSurface = TTF_RenderText_Solid(textFont, "TIME:", SDL_Color({0XFF, 0xFF, 0XFF}));
timeStringTexture = SDL_CreateTextureFromSurface(renderer, timeStringSurface);
texturesSizes[TIME_STRING] = std::make_pair(timeStringSurface->w, timeStringSurface->h);
SDL_Surface* movesStringSurface = TTF_RenderText_Solid(textFont, "MOVES:", SDL_Color({0XFF, 0xFF, 0XFF}));
movesStringTexture = SDL_CreateTextureFromSurface(renderer, movesStringSurface);
texturesSizes[MOVES_STRING] = std::make_pair(movesStringSurface->w, movesStringSurface->h);
pixelFont = TTF_OpenFont("data/fonts/pixels.ttf", 40);
if (pixelFont == NULL){
printf("Couldn't load data/fonts/pixels.ttf font! TTF_Error: %s", TTF_GetError());
}
else{
bool loadedAllImages = true;
for (int i=1; i<=nbImages; i++){
std::ostringstream sm;
sm << "data/images/examples/image" << i << ".bmp";
SDL_Surface* image = SDL_LoadBMP(sm.str().c_str());
if (image == NULL){
printf("Couldn't load %s! SDL_Error: %s", sm.str().c_str(), SDL_GetError());
loadedAllImages = false;
break;
}
else{
image = SDL_ConvertSurface(image, image->format, 0);
SDL_Surface* scaledImage = SDL_CreateRGBSurface(0, CEN_W, CEN_H, 32, 0, 0, 0, 0);
SDL_Rect* stretchRect = new SDL_Rect({0, 0, CEN_W, CEN_H});
SDL_BlitScaled(image, NULL, scaledImage, stretchRect);
bgImages[i] = SDL_CreateTextureFromSurface(renderer, scaledImage);
}
}
if (loadedAllImages){
bgImage = bgImages[rand() % nbImages + 1];
for (int i=0; i<MAX_X * MAX_Y - 1; i++){
numbersContsRectsCrops[i] = new SDL_Rect({
numbersContsRectsPos[0][0]->w * (i % MAX_Y),
numbersContsRectsPos[0][0]->h * (i / MAX_Y),
numbersContsRectsPos[0][0]->w,
numbersContsRectsPos[0][0]->h});
}
SDL_Surface* restartButton = SDL_LoadBMP("data/images/restart.bmp");
if (restartButton == NULL){
printf("Couldn't load data/images/restart.bmp! SDL_Error: %s)", SDL_GetError());
}
else{
restartButton = SDL_ConvertSurface(restartButton, restartButton->format, 0);
restartButtonTexture = SDL_CreateTextureFromSurface(renderer, restartButton);
restartButtonRect = new SDL_Rect({(CEN_X + CEN_W + WIN_W - restartButton->w) / 2,
CEN_Y,
restartButton->w,
restartButton->h});
updateTime();
updateMoves();
success = true;
}
}
}
}
}
return success;
}
void shuffle(std::vector<int> *arr){
srand(time(0));
std::vector<int> workspace;
for (int i=0; i<MAX_X*MAX_Y-1; i++){
workspace.push_back(i);
}
for (int i=0; i<MAX_X*MAX_Y-1; i++){
int rv = rand() % (MAX_X * MAX_Y - 1 - i);
arr->push_back(workspace[rv]);
workspace.erase(workspace.begin() + rv);
}
}
void initNumbers(){
std::vector<int> shuffledNumbers;
shuffle(&shuffledNumbers);
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
numbers[x][y] = shuffledNumbers[x + y * MAX_X];
}
}
numbers[MAX_X-1][MAX_Y-1] = -1;
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
winNumbers[x][y] = x + y * MAX_X;
numbersContsRectsPos[x][y] = new SDL_Rect({CEN_X + SEP_W + ((CEN_W - SEP_W) / MAX_X) * x,
CEN_Y + SEP_W + ((CEN_H - SEP_W) / MAX_Y) * y,
(CEN_W - SEP_W) / MAX_X - SEP_W,
(CEN_H - SEP_W) / MAX_Y - SEP_W});
}
}
winNumbers[MAX_X][MAX_Y] = -1;
}
bool init(){
bool success = false;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0){
printf("SDL couldn't init! SDL_Error: %s\n", SDL_GetError());
}
else if (TTF_Init() < 0){
printf("TTF couldn't init! TTF_Error: %s\n", TTF_GetError());
}
else{
window = SDL_CreateWindow("Sliding Puzzle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_W, WIN_H, SDL_WINDOW_SHOWN);
if (window==NULL){
printf("SDL Window couldn't be created! SDL_Error: %s\n", SDL_GetError());
}
else{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL){
printf("SDL Renderer couldn't be created! SDL_Error: %s\n", SDL_GetError());
}
else{
SDL_Surface* icon = SDL_LoadBMP("data/images/icon.bmp");
if (icon == NULL){
printf("Couldn't load data/images/icon.bmp! SDL_Error: %s\n", SDL_GetError());
}
else{
SDL_SetWindowIcon(window, icon);
initNumbers();
success = initTextures();
}
}
}
}
return success;
}
void mouseClick(int mouseX, int mouseY){
int rx1 = restartButtonRect->x;
int ry1 = restartButtonRect->y;
int rx2 = restartButtonRect->w + rx1;
int ry2 = restartButtonRect->h + ry1;
if (mouseX >= rx1 && mouseX <= rx2 &&
mouseY >= ry1 && mouseY <= ry2){
std::vector<int> shuffledNumbers;
shuffle(&shuffledNumbers);
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
numbers[x][y] = shuffledNumbers[x + y * MAX_X];
}
}
numbers[MAX_X-1][MAX_Y-1] = -1;
currentTime = -1;
updateTime();
timeFraction = 0;
nbMoves = 0;
bgImage = bgImages[rand() % nbImages + 1];
}
else{
std::pair<int, int> collCoor = {-1, -1};
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
if (numbers[x][y] != -1){
int nbx1 = numbersContsRectsPos[x][y]->x;
int nby1 = numbersContsRectsPos[x][y]->y;
int nbx2 = numbersContsRectsPos[x][y]->w + nbx1;
int nby2 = numbersContsRectsPos[x][y]->h + nby1;
if (mouseX >= nbx1 && mouseX <= nbx2 &&
mouseY >= nby1 && mouseY <= nby2){
collCoor.first = x;
collCoor.second = y;
}
}
}
}
if (collCoor.first != -1){
std::pair<int, int> swapCoor = {-1, -1};
for (int x=-1; x<=1; x+=2){
if (collCoor.first + x >= 0 && collCoor.first + x <MAX_X){
if (numbers[collCoor.first + x][collCoor.second] == -1){
swapCoor.first = collCoor.first + x;
swapCoor.second = collCoor.second;
}
}
}
for (int y=-1; y<=1; y+=2){
if (collCoor.second + y >= 0 && collCoor.second + y <MAX_Y){
if (numbers[collCoor.first][collCoor.second + y] == -1){
swapCoor.first = collCoor.first;
swapCoor.second = collCoor.second + y;
}
}
}
if (swapCoor.first != -1){
std::swap(numbers[collCoor.first][collCoor.second], numbers[swapCoor.first][swapCoor.second]);
updateMoves();
}
}
}
}
int main(int argc, char **argv){
if (init()){
bool quit = false;
stop = false;
SDL_Event e;
int mouseX, mouseY;
while (!quit){
while (SDL_PollEvent(&e) != 0){
switch(e.type){
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
SDL_GetMouseState(&mouseX, &mouseY);
mouseClick(mouseX, mouseY);
break;
}
}
SDL_SetRenderDrawColor(renderer, 0XFF, 0X6B, 0X6B, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_Rect* fillRect = new SDL_Rect({CEN_X, CEN_Y, CEN_W, CEN_H});
SDL_SetRenderDrawColor(renderer, 0XFF, 0XFF, 0XFF, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, fillRect);
SDL_SetRenderDrawColor(renderer, 0XFF, 0XFF, 0XFF, SDL_ALPHA_OPAQUE);
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
if (numbers[x][y] != -1){
//SDL_RenderFillRect(renderer, numbersContsRectsPos[x][y]);
SDL_RenderCopy(renderer, bgImage,
numbersContsRectsCrops[numbers[x][y]],
numbersContsRectsPos[x][y]);
}
}
}
SDL_Rect* numberRect;
for (int x=0; x<MAX_X; x++){
for (int y=0; y<MAX_Y; y++){
if (numbers[x][y] != -1){
numberRect = new SDL_Rect({numbersContsRectsPos[x][y]->x + 5,
numbersContsRectsPos[x][y]->y + 5,
numbersTexturesSizes[numbers[x][y]].first,
numbersTexturesSizes[numbers[x][y]].second});
SDL_RenderCopy(renderer, numbersTextures[numbers[x][y]], NULL, numberRect);
}
}
}
SDL_Rect* posRect;
posRect = new SDL_Rect({10, 10, texturesSizes[TIME_STRING].first, texturesSizes[TIME_STRING].second});
SDL_RenderCopy(renderer, timeStringTexture, NULL, posRect);
posRect = new SDL_Rect({10, posRect->y + texturesSizes[TIME_STRING].second + 5,
texturesSizes[TIME_INT].first, texturesSizes[TIME_INT].second});
SDL_RenderCopy(renderer, timeIntTexture, NULL, posRect);
posRect = new SDL_Rect({10, posRect->y + texturesSizes[TIME_INT].second + 15,
texturesSizes[MOVES_STRING].first, texturesSizes[MOVES_STRING].second});
SDL_RenderCopy(renderer, movesStringTexture, NULL, posRect);
posRect = new SDL_Rect({10, posRect->y + texturesSizes[MOVES_STRING].second + 5,
texturesSizes[MOVES_INT].first, texturesSizes[MOVES_INT].second});
SDL_RenderCopy(renderer, movesIntTexture, NULL, posRect);
SDL_RenderCopy(renderer, restartButtonTexture, NULL, restartButtonRect);
SDL_RenderPresent(renderer);
SDL_ShowWindow(window);
SDL_Delay(10);
if (!stop){
timeFraction += 10;
if (timeFraction == 1000){
updateTime();
timeFraction = 0;
}
if (winNumbers == numbers){
stop = true;
}
}
}
}
SDL_Quit();
return 0;
}
最佳答案
刚刚修好了。错误非常简单:我尝试访问的winNumbers
的长度为MAX_X
的长度,索引为MAX_X
(应该为MAX_X - 1
)。但是,我不知道为什么调试器没有检测到错误。
关于c++ - 调用SDL_CreateTextureFromSurface时SDL崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61176380/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!