Window API GameMap

本文介绍了一个简单的Windows程序,用于加载不同的地图图像并将其绘制到窗口中。通过使用位图资源文件和GDI函数,实现了地图图像的加载和显示。此外,还展示了如何创建兼容设备上下文以提高绘图效率。

 #include "GameMap.h"


int WINAPI WinMain(
                   HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR  lpCmdLine,
                   int nShowCmd
                   )
{
    MSG msg;
    HDC hdc;
    int i;
    char filename[20] = "";

    MyRegisterClass(hInstance);

    InitInstance(hInstance, nShowCmd);

    for(i = 0; i < 3; i++)
    {
        sprintf(filename, "Image/map%d.bmp", i);
        map[i] = (HBITMAP)LoadImage(hInstance, filename, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
    }
    
    

    hdc = GetDC(hWnd);
    
    MyPaint(hdc);
    ReleaseDC(hWnd, hdc);


    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}


//****************************************
//
//  注册函数
//
//****************************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wce;
    
    wce.cbSize = sizeof(wce);
    wce.style = CS_VREDRAW|CS_HREDRAW;
    wce.lpfnWndProc = MyProc;
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hInstance = hInstance;
    wce.hIcon = NULL;
    wce.hCursor = LoadCursor(NULL, IDC_CROSS);
    wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wce.lpszMenuName = NULL;
    wce.lpszClassName = "MyWClass";
    wce.hIconSm = NULL;

    RegisterClassEx(&wce);

    return 0;
    
}


//****************************************
//
//  创建窗体函数
//
//****************************************
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{    
    hinst = hInstance;

    hWnd = CreateWindow("MyWClass", "coco", WS_SYSMENU, 100, 100, 300, 350, NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    
    
    return TRUE;
}


//****************************************
//
//  消息处理函数
//
//****************************************
LRESULT CALLBACK MyProc(
                        HWND hWnd,
                        UINT uMsg,
                        WPARAM wParam,
                        LPARAM lParam
                        )
{
    HDC hdc;
    PAINTSTRUCT ps;    

    switch(uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        MyPaint(hdc);
        EndPaint(hWnd, &ps);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}


//****************************************
//
//  绘图函数
//
//****************************************
void MyPaint(HDC hdc)
{   
    mdc = CreateCompatibleDC(hdc);
    fulldc = CreateCompatibleDC(hdc);
    fullmap = CreateCompatibleBitmap(hdc, 300, 300);

    int i, x, y;
    int mapIndex[rows*cols] = {0, 1, 1,
                          0, 1, 1,
                          2, 2, 2
                           };

    SelectObject(fulldc, fullmap);

    for(i = 0; i < 9; i++)
    {
        x = i%rows * 100;
        y = i/3 *100;

        SelectObject(mdc, map[mapIndex[i]]);
        BitBlt(fulldc, x, y, 100, 100, mdc, 0, 0, SRCCOPY);
    }


   SelectObject(fulldc, fullmap);
   BitBlt(hdc, 0, 0, 300, 300, fulldc, 0, 0, SRCCOPY);
   

   DeleteDC(mdc);
   DeleteDC(fulldc);
   DeleteObject(fullmap);

}

### C语言游戏开发常用函数库 对于C语言游戏开发而言,选择合适的函数库至关重要。以下是几个常用的函数库: #### Ncurses 库 Ncurses 是一个强大的字符界面处理库,在终端环境下创建交互式的文本应用非常有用。该库支持基本的窗口操作、颜色设置、鼠标事件捕捉等功能[^2]。 ```c #include <ncurses.h> int main() { initscr(); /* 初始化屏幕 */ printw("Hello, world!");/* 打印字符串到当前光标位置 */ refresh(); /* 刷新屏幕以显示更改 */ getch(); /* 等待按键输入 */ endwin(); /* 结束 ncurses 模式 */ return 0; } ``` #### SDL (Simple DirectMedia Layer) 库 SDL 提供了一个跨平台的方式访问音频、键盘、鼠标、触摸屏以及图形硬件接口。它非常适合用来构建多媒体应用程序尤其是视频游戏。其特点是易于上手且功能强大[^1]。 ```c #include "SDL.h" // 初始化 SDL 子系统并创建窗口... if(SDL_Init(SDL_INIT_VIDEO) != 0){ printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } SDL_Window* win = SDL_CreateWindow("Game Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN); // ...其他代码... SDL_Quit(); return 0; ``` #### Allegro 库 Allegro 是另一个专为游戏开发者设计的多用途库,提供了丰富的图像绘制、声音播放、输入设备读取等API调用。这个库具有良好的文档和支持社区。 ```c #include <allegro5/allegro.h> #include <allegro5/allegro_primitives.h> int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; if(!al_init()) { // Initialize allegro. fprintf(stderr, "failed to initialize allegro!\n"); return -1; } display = al_create_display(640, 480); if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } // 绘制矩形框作为示例 al_draw_rectangle(10, 10, 790, 590, al_map_rgb(255, 0, 0), 3.0f); al_flip_display(); al_rest(10.0); // 屏幕停留时间 al_destroy_display(display); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值