在构建SDL项目时遇到问题
Having trouble building SDL Project
我已经开始研究SDL了。我在一个基本程序上遇到了麻烦,每次编译它都没有错误,但程序完全是空白的。构建成功,应用程序图标弹出到我的dock中,但保持打开5秒钟,没有窗口。代码如下:
//
// main.cpp
// HelloSDL
//
// Created by Liam Tan
// Copyright 2014 Liam Tan. All rights reserved.
//
#include <stdlib.h>
#include <SDL/SDL.h>
#include <string>
#include <iostream>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//Declares surfaces
SDL_Surface* message = NULL;
SDL_Surface* background = NULL;
SDL_Surface* screen = NULL;
SDL_Surface *load_image( std::string filename) {
//declaration
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
//takes arguement and loads that BMP
loadedImage = SDL_LoadBMP(filename.c_str() );
//Checks, swaps opti with loaded then frees loaded
if (loadedImage!= NULL) {
optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
//temporary rectangle to hold offsets
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, NULL);
}
int SDL_main (int argc, char **argv) {
const std::string errorMsg = "failed to initialize. ";
//Initialise
if( SDL_Init(SDL_INIT_EVERYTHING) == -1 ) {
std::cout << "SDL " << errorMsg << std::endl;
SDL_GetError();
return 1;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL) {
std::cout << "Video mode " << errorMsg << std::endl;
SDL_GetError();
return 1;
}
SDL_WM_SetCaption("Programming", NULL);
//loads message
message = load_image("textures/hello.bmp");
//loads background
background = load_image("textures/background.bmp");
//blits 4 times!
apply_surface(0,0,background,screen);
apply_surface( 320, 0, background, screen );
apply_surface( 0, 240, background, screen );
apply_surface( 320, 240, background, screen );
SDL_Delay(5000);
SDL_Quit();
return 0;
}
我使用的是Xcode 5和SDL 1.2。
我在您的代码中看到了一些问题。它们可能并不全是错误。
1) 当你闪电般地扫描一个图像时,它不会直接进入屏幕。它被保存在缓冲区中,直到您调用SDL_Flip(screen);
所以最后几行应该像一样
...
apply_surface( 320, 240, background, screen );
SDL_Flip(screen);//Display the results of the blits
SDL_Delay(5000);
SDL_Quit();
return 0;
2) 在对图像进行闪电扫描时,不使用Rect offset
。它将始终显示在0,0位置。
//temporary rectangle to hold offsets
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, NULL);
3) 您永远不会释放加载的图像。您需要在SDL_Quit之前放入一些SDL_FreeSurface(...);
。
4) 您加载但从未使用hello.bmp
此外,我不明白为什么要将曲面声明为全局曲面。但也许你有充分的理由。
您也可以尝试用替换SDL_Delay(5000);
bool done = false;
while (!done)//we wait until done == true
{
SDL_Event event;
while (SDL_WaitEvent(&event))//we get all the events
{
switch (event.type) //we look what type of event we received
{
case SDL_QUIT: //We received a quit event
done = true;//we want to exit the loop
break;
}
}
}
因为SDL_Delay告诉您的计算机您的应用程序在一段时间内不需要更新。
相关文章:
- 编译包含字符串的代码时遇到问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题
- 存储另一个函数返回的布尔数组时遇到问题
- 我在 OpenGL 中显示图像时遇到问题
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 在 for 循环中更新两个变量时遇到问题C++
- 为什么我的程序在打开网络设备时遇到问题
- 在 Tensorflow C++ 中使用 FIFOQueue 时遇到问题
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?