注册 登录
编程论坛 C图形专区

用C语言编写具有图形界面的小游戏用什么编译器好?

雪狼MJ 发布于 2012-07-08 09:18, 5435 次点击
这一学期学了C语言的基础,现在可以用C编写可以处理数据的小程序了,但一直没有编过有图形界面的程序
一直在用C-Free编译器,这个适用于初学者,容易上手嘛。
但现在想编有图形界面的程序了,感觉这个编译器不怎么好用了,这个编译器能编有图形界面的程序吗?有什么编译器更好呢?
老鸟们,求推荐!!!
5 回复
#2
jokerskill2012-07-08 19:30
你的贴怎么跑到这里来了???跳到顶上的贴是怎么发的???

#3
hellovfp2012-07-09 11:43
偶测试了一下,可以把下面的三行拷到一个bat文件中,再把这个文件拷到cfree/mingw目录中。
然后进入cfree/mingw目录,运行一下这个bat批处理。

@echo.
@echo Setting up environment for using MinGW with GCC from %~dp0.
@set PATH=%~dp0bin;%PATH%

最后用命令行gcc -mwindows main.c -o main.exe可以编译下面的代码:

可以考虑换成codeblocks/codelite,都是同样的编译器,在库的支持上比c-free要好,c-free中添加libgdi32.a会出问题。

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

//////////////////////////////////////////////////
//模拟库中的inigraph, closegraph, lineto函数
void init_graphics(int width, int height);
void close_graphics();
void draw_line(int x, int y, int x1, int y1, COLORREF color);

unsigned long _stdcall ThreadFunc(void *);
LRESULT CALLBACK main_proc(HWND, UINT, WPARAM, LPARAM);

//////////////////////////////////////////////////
HANDLE hwin_main;
DWORD ThreadID;
HWND hwnd_main;
HDC hdc_main;
HDC hdc_buff = 0;
HBITMAP hmap = 0;
int main_w, main_h;
UINT draw_timer;
HWND console;

/////////////////////////////////////////////////////////////////////////
// 模拟EasyX的Graphics图形库运行流程,主函数看着是不是有点TC代码的熟悉?
/////////////////////////////////////////////////////////////////////////
int main()
{
    int i;

    init_graphics(640, 480);
    srand( time(NULL) );
   
    for(i = 0; i < 120; i++)
    {
        draw_line(200, 200, rand()% 640 - (rand() % 100), rand() % 480 + rand() % 100, RGB(70, 40, 128));
        Sleep(20);
    }

    draw_line(30, 30, 100, 100+i, RGB(255, 0, 128));
    draw_line(40, 130, 100, 100+i, RGB(255, 0, 128));

    printf("start waitting.....\n");
    WaitForSingleObject(hwin_main, INFINITE);
    printf("waitting end.....\n");

    close_graphics();
    return 0;
}

unsigned long _stdcall ThreadFunc(void *lpvoid)
{
    MSG msg;
    WNDCLASS wnd;

    memset(&wnd, 0, sizeof(wnd));
    wnd.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
    wnd.hInstance = GetModuleHandle(NULL);
    wnd.lpfnWndProc = main_proc;
    wnd.lpszClassName = "EasyWindow";
    wnd.style = CS_HREDRAW | CS_VREDRAW;

    if(! RegisterClass(&wnd) ) fprintf(stderr, "register class error!\n");

    hwnd_main = CreateWindow("EasyWindow", "EasyWindow Create",
                WS_OVERLAPPEDWINDOW,
                10, 10, main_w, main_h,
                NULL,
                NULL,
                wnd.hInstance,
                0);
    if(! hwnd_main ) fprintf(stderr, "Create windows error!\n");

    ShowWindow(hwnd_main, SW_SHOWNORMAL);
    UpdateWindow(hwnd_main);

    hdc_main = GetDC(hwnd_main);
    hdc_buff = CreateCompatibleDC(NULL);
    hmap = CreateCompatibleBitmap(hdc_main, main_w, main_h);
    SelectObject(hdc_buff, hmap);
    ReleaseDC(hwnd_main, hdc_main);

    SetTimer(hwnd_main, draw_timer, 30, NULL);

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

LRESULT CALLBACK main_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    switch(uMsg)
    {
        case WM_CHAR:
            //SendMessage(console, WM_CHAR, wParam, lParam);
        case WM_DESTROY:
            DeleteDC(hdc_buff);
            DeleteObject(hmap);
            KillTimer(hwnd_main, draw_timer);
            PostQuitMessage(0);
            break;
        case WM_TIMER:
            InvalidateRect(hwnd_main, NULL, FALSE);
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            BitBlt(hdc, 0, 0, main_w, main_h, hdc_buff, 0, 0, SRCCOPY);
            EndPaint(hwnd, &ps);
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

void init_graphics(int width, int height)
{
    main_w = width;
    main_h = height;
    hwin_main=CreateThread(NULL,0,
                ThreadFunc,NULL,
                0,&ThreadID);

    Sleep(20);
    /* 下面这段可以隐藏控制台窗口 */
    console = FindWindow("ConsoleWindowClass", NULL);
    if(console)
        ShowWindow(console, SW_HIDE);
    else
        fprintf(stderr, "can't find console windows\n");

}

void close_graphics()
{
    CloseHandle(hwin_main);
    ShowWindow(console, SW_SHOWNORMAL);
}

void draw_line(int x, int y, int x1, int y1, COLORREF color)
{
    HPEN pen, old_pen;

    pen = CreatePen(PS_SOLID, 2, color);
    old_pen = (HPEN) SelectObject(hdc_buff, pen);
    MoveToEx(hdc_buff, x, y, NULL);
    LineTo(hdc_buff, x1, y1);
    DeleteObject(old_pen);
}

[ 本帖最后由 hellovfp 于 2012-7-9 13:01 编辑 ]
#4
二少爷2012-07-24 00:22
... ...
#5
Agdmeg2012-08-16 12:53
Tc可以
#6
c简2012-10-30 21:52
turbo c 3.0挺好的哇!
1