标签: sdl

ISO C++禁止声明'游戏'没有类型 - 可能包括问题?

编译输出:

g++ -Wall -g main.cpp `sdl-config --cflags --libs` -lSDL_mixer
In file included from Game.h:8,
                 from main.cpp:1:
DrawableObject.h:11: error: ISO C++ forbids declaration of ‘Game’ with no type
DrawableObject.h:11: error: expected ‘;’ before ‘*’ token
DrawableObject.h:13: error: expected ‘)’ before ‘*’ token
main.cpp:7: error: expected ‘}’ at end of input
main.cpp:7: error: expected unqualified-id at end of input
make: *** [all] Error 1
brett@brett-laptop:~/Desktop/SDL$ make
g++ -Wall -g main.cpp `sdl-config --cflags --libs` -lSDL_mixer
In file included from Game.h:8,
                 from main.cpp:1:
DrawableObject.h:11: …
Run Code Online (Sandbox Code Playgroud)

c++ sdl

0
推荐指数
1
解决办法
3118
查看次数

SDL_SetColorKey的问题

我正在尝试使用SDL创建透明精灵.我在带有品红色(0xff00ff)背景的位图上使用SDL_SetColorKey(它是100%品红色,我用GIMP检查了:))对SDL_SetColorKey的调用如下所示:

SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );

对SDL_SetColorKey的调用显然返回0,但是没有透明度.谁能告诉我这里缺少什么?

如果有人想测试它,这是有问题的代码:

#include "SDL/SDL.h"

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";

int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, 
      SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );

   SDL_Surface* bitmap = SDL_LoadBMP("resources/ship.bmp");
   if(SDL_SetColorKey( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) )) printf("aaaaa %s", SDL_GetError());



   // Part of the screen we want to draw the sprite to …
Run Code Online (Sandbox Code Playgroud)

c++ graphics transparency sdl

0
推荐指数
1
解决办法
3182
查看次数

'glGetTexLevelParameter'未在此范围内声明

好吧,所以在任何人问之前,是的我确实<gl/gl.h>包括在内.任何人都知道为什么这个功能glGetTexLevelParameter没有出现?

c++ opengl sdl

0
推荐指数
1
解决办法
181
查看次数

这个for循环我做错了什么?

我正在使用SDL处理一段C代码,它应该用黑白像素填充屏幕......它使用for循环填充屏幕和一个随机数来决定每个像素是黑色还是白色.

它开始按原样工作一秒钟,但它会慢慢开始显示越来越多的白色像素,直到屏幕完全变白并保持这种状态.我不明白我在这里做错了什么.我有它在控制台中打印num的数字,它继续显示随机的1和0,就像屏幕是白色的那样,所以我认为问题存在于if else语句或for循环的某处.

这是相关代码......

 /*outside main program loop*/
 17   int x, y, num;
 18   srand(time(0));

 /*inside main program loop*/
 28     for(y=0;y<=480;y++) { 
 29       for(x=0;x<=640;x++) {
 30         num = rand() % 2;
 31         if(num == 0) {
 32           pixelRGBA(screen, x, y, 255, 255, 255, 255);
 33         }
 34         else {
 35           pixelRGBA(screen, x, y, 0, 0, 0, 0);
 36         }
 37       }
 38     } 
 39     SDL_Flip(screen);
Run Code Online (Sandbox Code Playgroud)

c random for-loop sdl

0
推荐指数
1
解决办法
69
查看次数

SDL不在本机环境中编译

我正在尝试用SDL编译这段代码

#include <SDL.h>
int main(int argc, char * argv[]){
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

文件本身编译得很好

g++ -c main.cpp -ISDL/include
Run Code Online (Sandbox Code Playgroud)

但是使用g ++在cygwin中使用以下命令进行编译

g++ -o test main.o  -lSDL2 -lSDL2main -L SDL/lib/x64
Run Code Online (Sandbox Code Playgroud)

产生这个巨大的错误......在我看来,这似乎是SDL本身的问题......

$ g++ -o test main.o  -lSDL2 -lSDL2main -L SDL/lib/x64
Warning: corrupt .drectve at end of def file
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0xe): undefined reference to `SDL_SetMainReady'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0xe): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_SetMainReady'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x29): undefined reference to `SDL_wcslen'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x29): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SDL_wcslen'
SDL/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[WinMain]+0x46): undefined reference to `SDL_iconv_string' …
Run Code Online (Sandbox Code Playgroud)

c++ cygwin sdl g++ sdl-2

0
推荐指数
1
解决办法
2023
查看次数

SDL_Rect:在屏幕中心获取矩形的公式是什么?

我目前正在尝试SDL并且想知道如何在屏幕中心获得一个矩形.

例如,假设我有一个640x480的屏幕,我想要一个矩形,其宽度和高度分别是屏幕中间的那些分辨率的一半.我知道会是:

SDL_Rect centerRect = { 640 / 4, 480 / 4, 640 / 2, 480 / 2 };

是否有一个公式可以让我每次在中间放置任何宽度和高度的矩形?

注意:我不是要找到矩形中心点的坐标.只需要左上角的坐标,我必须经过的就是屏幕和矩形的宽度和高度.

sdl

0
推荐指数
1
解决办法
978
查看次数

c错误:case标签不会减少为整数常量

这是错误所关注的开关案例:

switch(event.type)
        {
        case SDL_Quit:
            for (int i=0;i<NUMMENU;i++)
                SDL_FreeSurface(menus[i]);
            return 1;

        case SDL_MOUSEMOTION:
            x=event.motion.x;
            y=event.motion.y;
            for(int i=0;i<NUMMENU;i++)
            {
                if(x>=position[i].x && x<=position[i].x+position[i].w && y>=position[i].y && y<=position[i].y+position[i].h)
                    {
                    if(!selected[i])
                    {
                        selected[i]=1;
                        SDL_FreeSurface(menus[i]);
                        menus[i]=TTF_RenderText_Solid(font,labels[i],color[1]);
                    }
                }else{
                        if(selected[i])
                        {
                            selected[i]=0;
                            SDL_FreeSurface(menus[i]);
                            menus[i]=TTF_RenderText_Solid(font,labels[i],color[0]);
                        }
                      }
            }

        case SDL_MOUSEBUTTONDOWN:
            x=event.button.x;
            y=event.button.y;
            for(int i=0;i<NUMMENU;i++)
                if(x>=position[1].x && x<=position[1].x+position[i].w && y>=position[i].y && y<=position[i].y+position[i].h)
                {
                    for(int j=0;j<NUMMENU;j++)
                        SDL_FreeSurface(menus[j]);
                    return i;
                }
                break;
        case SDL_KEYDOWN:
            if(event.key.keysym.sym==SDLK_ESCAPE)
            {
                for(int i=0;i<NUMMENU;i++)
                    SDL_FreeSurface(menus[i]);
                return 0;
            }

        }
Run Code Online (Sandbox Code Playgroud)

我有这个错误,我不知道为什么以及我该怎么做才能解决这个问题.我试图添加休息; 在每个案件的最后但没有结果.

如果您需要更多代码或更多信息,我可以给予精确度.

问候,

c sdl switch-statement

0
推荐指数
1
解决办法
1311
查看次数

C++错误期望'{'标记之前的类名

我知道很多,其他很多人已经发布了这个但是我很迷茫,因为有很多人说前向声明和其他大量令人困惑的东西我似乎无法找到一种方法来使用而不会在我的编译器中出现新的错误所以如果有人能帮助我,将非常感激.

我在Ubuntu 15.04上使用Code :: Blocks

错误是 OptimizedSurface.h|11|error: expected class-name before ‘{’ token

在OptimizedSurface Header文件中我决定这样做,class OptimizedSurface : Game{因为在此错误之前我得到了

include/Game.h|18|error: invalid use of member ‘Game::windowSurface’ in static member function 该行是optimize_surface = SDL_ConvertSurface(surface,Game :: windowSurface-> format,0);

现在代码是以下文件.

main.cpp -

#include "Game.h"
#include <stdio.h>


int main(int argc, char* args[]){
    printf("Initializing Game class\n");
    Game game = Game();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Game.h -

#ifndef GAME_H
#define GAME_H

#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include <stdio.h>
#include <chrono>
#include <thread>
#include <iostream>
#include "MenuState.h"

class MenuState;

class …
Run Code Online (Sandbox Code Playgroud)

c++ sdl compiler-errors derived-class

0
推荐指数
1
解决办法
1万
查看次数

是否可以在不使用嵌入式while_loop的情况下使用SDL检查事件?

我最初使用glfw编写游戏。但是由于缺少android的可移植性,我不得不用SDL替换所有glfw代码,因为SDL更易于移植。

我使用glfw框架工作的原始游戏循环非常出色。这里是:

// game loop
while (!glfwWindowShouldClose(window)) {
    //if-else statements that create  
    //the games state machine
    //player input is received here
    //rendering done here
}
Run Code Online (Sandbox Code Playgroud)

好。编写此游戏循环可能有更好的方法,但重点是适用于游戏原型的代码。

但是,现在我切换到SDL时遇到了一个问题。似乎必须将SDL_Event放入while循环中以不断检查事件。并且此while_loop在另一个while_loop中。所以我的代码看起来像这样:

while(!Quit) {
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        //if-else statements that create  
        //the games state machine
        //player input is received here
        //rendering done here
    }
}
Run Code Online (Sandbox Code Playgroud)

但是while_loop中的while_loop完全弄乱了游戏的渲染!我现在忽隐忽现!是否有其他方法可以像我之前使用glfw一样在单个while_loop中表达此代码。还是有任何方法可以在不使用嵌入式while_loop的情况下检查事件

c c++ sdl glfw game-loop

0
推荐指数
1
解决办法
423
查看次数

使用SDL在C++中创建图形类

我正在尝试用C++创建一个洞穴故事克隆的游戏窗口,所以首先,我创建下面的头文件,之后,我去创建类文件.当我完成该类时,我一直收到参数类型为sdl_window和sdl_render的参数类型不完整的错误.如果有人能帮助我弄清楚我做错了什么.

到Graphics.h

#ifndef GRAPHICS.h

#define GRAPHICS.h

struct SDL_window;
struct SDL_render;

class Graphics {
    public:
        Graphics();
        ~Graphics();
    private:
        SDL_window* window = NULL;
        SDL_render* render = NULL;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Graphics.cpp

#include <SDL.h>

#include "graphics.h"


/* Graphics class
* Holds all information dealing with graphics for the game
*/

Graphics::Graphics() {
    SDL_CreateWindowAndRenderer(640, 480, 0, &window, &render);
    SDL_SetWindowTitle(window, "Cavestory");
}

Graphics::~Graphics() {
    SDL_DestroyWindow(window);
}
Run Code Online (Sandbox Code Playgroud)

c++ sdl visual-studio

0
推荐指数
1
解决办法
207
查看次数