2013-10-04 96 views
3

我很難設置SDL2 Code :: Blocks 我嘗試了一些在Google上找到的教程,並且我試圖通過在此網站上搜索來解決問題,但是我未能解決它每當我編譯一個簡單的程序時,我得到的錯誤 所以我希望有人能夠給我一個簡單的一步一步的指導,幫助我 首先讓我知道,如果code :: blocks和sdl 2工作在一起?如何使用sdl2與CoDeBlocks

那麼我不得不說,我使用的是HP便攜與Windows 7 - 64個BITS 我代碼:: Blocks的12.11與embbeded MINGW32 4.7.1它 安裝,我想我的鏈接庫和包括正確的文件對我的設置,但仍然沒有得到的東西的工作,我即將從SDL2-2.0.1 \放棄

+0

你可以更新錯誤,你得到你的問題嗎? – Mars

回答

3
  1. 下載SDL2-devel的-2.0.1-mingw.tar.gz
  2. 解壓i686-w64-mingw32這些目錄:\ bin,\ include,\ lib和\ share到例如X:\代碼塊\ MinGW的\
  3. 創建目錄%APPDATA%\代碼塊\ UserTemplates \ SDL2和粘貼文件SDL2.cbp,main.cpp中,並cb.bmp

SDL2.cbp:

注:編輯您的驅動器和路徑X的信:\代碼塊,因爲你需要

<?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> 

的main.cpp

#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