gpt4 book ai didi

codeblocks - 如何将 sdl2 与 CoDeBlocks 一起使用

转载 作者:行者123 更新时间:2023-12-02 00:04:39 24 4
gpt4 key购买 nike

我很难用 Code::Blocks 设置 SDL2
我尝试了一些在谷歌上找到的教程,我也尝试通过在这个网站上搜索来解决问题,但是我没有解决它,每次我在编译一个简单的程序时遇到错误
所以我希望有人好心给我一个简单的分步指南来帮助我
首先让我知道 code::blocks 和 sdl 2 是否一起工作?

那么我不得不说,我使用的是带有 Windows 7 - 64 BITS 的 HP 可移植计算机
我得到了 Code::Blocks 12.11,上面安装了嵌入式 Mingw32 4.7.1
我想我在我的设置中正确链接了库和包含文件,但仍然没有工作,我即将放弃

最佳答案

  • 下载 SDL2-devel-2.0.1-mingw.tar.gz
  • 从 SDL2-2.0.1\i686-w64-mingw32 解压这些目录:\bin、\include、\lib 和\share 到例如X:\CodeBlocks\MinGW\
  • 创建目录 %APPDATA%\CodeBlocks\UserTemplates\SDL2 并粘贴文件 SDL2.cbp、main.cpp 和 cb.bmp

  • SDL2.cbp:

    注意:根据需要编辑驱动器号和路径 X:\CodeBlocks
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <CodeBlocks_project_file>
    <FileVersion major="1" minor="6" />
    <Project>
    <Option title="SDL2" />
    <Option pch_mode="2" />
    <Option compiler="gcc" />
    <Build>
    <Target title="Debug">
    <Option output="bin/Debug/SDL2" prefix_auto="1" extension_auto="1" />
    <Option object_output="obj/Debug/" />
    <Option type="1" />
    <Option compiler="gcc" />
    <Compiler>
    <Add option="-g" />
    </Compiler>
    </Target>
    <Target title="Release">
    <Option output="bin/Release/SDL2" prefix_auto="1" extension_auto="1" />
    <Option object_output="obj/Release/" />
    <Option type="0" />
    <Option compiler="gcc" />
    <Compiler>
    <Add option="-O2" />
    </Compiler>
    <Linker>
    <Add option="-s" />
    </Linker>
    </Target>
    </Build>
    <Compiler>
    <Add option="-Wall" />
    <Add directory="X:/CodeBlocks/MinGW/include" />
    </Compiler>
    <Linker>
    <Add library="mingw32" />
    <Add library="SDL2main" />
    <Add library="SDL2.dll" />
    <Add library="user32" />
    <Add library="gdi32" />
    <Add library="winmm" />
    <Add library="dxguid" />
    <Add directory="X:/CodeBlocks/MinGW/lib" />
    </Linker>
    <Unit filename="cb.bmp" />
    <Unit filename="main.cpp" />
    <Extensions>
    <code_completion />
    <envvars />
    <debugger />
    <lib_finder disable_auto="1" />
    </Extensions>
    </Project>
    </CodeBlocks_project_file>

    主文件
    #include <iostream>
    #include <SDL2/SDL.h>

    int main ( int argc, char** argv )
    {
    // initialize SDL video
    if (SDL_Init(SDL_INIT_VIDEO) == -1)
    {
    std::cout << "Unable to init SDL video: "<< SDL_GetError() << std::endl;
    return 1;
    }
    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    // create a new window
    SDL_Window* screen = NULL;
    screen = SDL_CreateWindow("CodeBlocs Logo Window",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    150,120,
    /* SDL_WINDOW_FULLSCREEN | */
    SDL_WINDOW_OPENGL);
    if ( NULL == screen )
    {
    std::cout << "Unable to create window: "<< SDL_GetError() << std::endl;
    return 1;
    }

    // Create a new renderer
    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(screen, -1, 0);

    if ( NULL == renderer )
    {
    std::cout << "Unable to create renderer: "<< SDL_GetError() << std::endl;
    return 1;
    }

    // load an image
    SDL_Surface* bitmap = NULL;
    bitmap = SDL_LoadBMP("cb.bmp");

    if ( NULL == bitmap )
    {
    std::cout << "Unable to load bitmap: "<< SDL_GetError() << std::endl;
    return 1;
    }

    SDL_Texture* texture = NULL;
    texture = SDL_CreateTextureFromSurface(renderer, bitmap);
    SDL_FreeSurface(bitmap);

    // program main loop
    bool done = false;
    while (!done)
    {
    // drawing
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
    // check for messages
    switch (event.type)
    {
    // exit if the window is closed
    case SDL_QUIT:
    done = true;
    break;

    // check for keypresses
    case SDL_KEYDOWN:
    {
    // exit if ESCAPE is pressed
    if (event.key.keysym.sym == SDLK_ESCAPE)
    done = true;
    break;
    }
    } // end switch
    } // end of message processing
    } // end main loop

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(screen);

    SDL_Quit();
    return 0;
    }

    cb.bmp 任何位图文件 150x120

    运行代码块和文件->新建->从模板...->用户模板->SDL2

    关于codeblocks - 如何将 sdl2 与 CoDeBlocks 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187080/

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