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

得分:15

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

偶测试了一下,可以把下面的三行拷到一个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

#include

#include

#include

#include

//

//模拟库中的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 编辑]

6f83fddf9cb9ff6843fffd45e1807199.gif

我们都在路上。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值