首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

【SDL 開發入門】Visual C++ 6.0 環境建置完整指南

     編輯:關於C語言

SDL (Simple DirectMedia Layer) 是廣受歡迎的跨平台多媒體庫。本文記錄在 Windows XP SP3 上,使用 Visual C++ 6.0 建置 SDL 1.2.14 開發環境的完整步驟,並附上可直接編譯執行的測試程式碼。

開發環境與下載

  • 作業系統:Microsoft Windows XP Service Pack 3
  • IDE:Microsoft Visual C++ 6.0
  • SDL 開發包:SDL-devel-1.2.14-VC6.zip(官網下載)

六步驟完成環境設定

  1. 解壓並複製庫檔
    解壓開發包,將 lib 資料夾下的 SDL.lib、SDLmain.lib 複製到 VC6 安裝目錄的 lib 資料夾。
  2. 部署執行階段 DLL
    將 SDL.dll 複製到 C:\WINDOWS\SYSTEM32。若需將程式移植到未安裝 SDL 的電腦,請將 SDL.dll 隨程式一併發布。
  3. 設定標頭檔路徑
    在 VC6 include 目錄下建立 SDL 子目錄,將開發包 include 內所有檔案複製進去。
  4. 建立專案並設定執行階段庫
    新建 Win32 Application 專案。開啟 Project → Settings → C/C++ → Code Generation,將 Use run-time library 設為 Multithread DLL。
  5. 連結器加入庫檔
    在 Link → Input → Object/library modules 填入 SDL.lib SDLmain.lib。
  6. 新增原始檔並編譯執行
    專案加入 .cpp 檔,即可編譯執行。

官方測試程式碼(可直接貼上編譯)

#include <stdlib.h>
#include "SDL/SDL.h"

SDL_Surface *screen;

void render()
{
    if (SDL_MUSTLOCK(screen)) {
        if (SDL_LockSurface(screen) < 0) return;
    }

    int tick = SDL_GetTicks();
    int i, j, yofs, ofs;

    yofs = 0;
    for (i = 0; i < 480; ++i) {
        for (j = 0, ofs = yofs; j < 640; ++j, ++ofs) {
            ((unsigned int*)screen->pixels)[ofs] = i * i + j * j + tick;
        }
        yofs += screen->pitch / 4;
    }

    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);

    SDL_UpdateRect(screen, 0, 0, 640, 480);
}

int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
    }
    atexit(SDL_Quit);

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    if (!screen) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }

    while (1) {
        render();

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_KEYDOWN: break;
                case SDL_KEYUP:
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        return 0;
                    break;
                case SDL_QUIT:
                    return 0;
            }
        }
    }
    return 0;
}

常見問題提示

  • 若編譯報找不到 SDL.h,請確認步驟 3 標頭檔路徑是否正確。
  • 連結錯誤 unresolved external symbol _SDL_main 通常是專案類型未選 Win32 Application 或未連結 SDLmain.lib。
  • 執行時彈出「找不到 SDL.dll」,請確認步驟 2 已將 DLL 放入系統目錄或程式同目錄。

相關連結

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved