⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 一个很好的SDL建立应用程序界面的例子
💻 C
📖 第 1 页 / 共 2 页
字号:
			case SDLK_DOWN:				incTListEntry(tbox_test, 1);				break;			case SDLK_PAGEUP:				decTListEntry(tbox_test, 10);				break;			case SDLK_PAGEDOWN:				incTListEntry(tbox_test, 10);				break;#ifdef DEBUG			case SDLK_F1:				dump_widget_list();				break;#endif			default:				break;		}	}}////		The Main Program//#ifdef	WIN32int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)#elseint main(int argc, char *argv[])#endif{	SDL_Event	event;#ifdef LINUX	bool				isX11 = FALSE;	int				x, y, w, h;	int				x11screen;	SDL_SysWMinfo	info;#endif	char		*cap = NULL;	BUTTON	*btn = NULL;	BUTTON	*tbbtn1 = NULL;	BUTTON	*tbbtn2 = NULL;	CHECKBOX	*chkb = NULL;	int		i, j, chr;	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)	{		printf("\nUnable to initialize SDL: %s\n", SDL_GetError());		return 1;	}	screen = SDL_SetVideoMode(SCREEN_PIXEL_WIDTH, SCREEN_PIXEL_HEIGHT, SCREEN_PIXEL_DEPTH, 0);	if (screen == NULL)	{		printf("\nUnable to set video mode: %s\n", SDL_GetError());		exit(1);	}	atexit(SDL_Quit);	atexit(widgetQuit);	cap = strstr(caption, "x");	sprintf(cap, "%d.%d.%d", VERNO, MAJORREV, MINORREV);	SDL_WM_SetCaption(caption, NULL);	SDL_EnableUNICODE(1);	SDL_EnableKeyRepeat(KB_DELAY, KB_INTERVAL);#ifdef LINUX					// put the window where we want it (won't work in WIN32 or OS X)	SDL_VERSION(&info.version);	if (SDL_GetWMInfo(&info) > 0)	{		if (info.subsystem == SDL_SYSWM_X11)			isX11 = TRUE;	}	if (isX11)	{		info.info.x11.lock_func();		x11screen = DefaultScreen(info.info.x11.display);		w = DisplayWidth(info.info.x11.display, x11screen);		h = DisplayHeight(info.info.x11.display, x11screen);		x = (w - screen->w) / 2;		if (x + screen->w > w)					// if centering x won't fit,			x = WINDOW_X_POSITION;				// use arbitrary horizontal position		y = (h - screen->h) / 2;		if (y + screen->h > h)					// if centering y won't fit,			y = WINDOW_Y_POSITION;				// use arbitrary vertical position		if (x >= 0 && y >= 0)			XMoveWindow(info.info.x11.display, info.info.x11.wmwindow, x, y);		info.info.x11.unlock_func();	}#endif	initWidgets();				// initialize widget video	menu = newMenu(0, 0, SCREEN_PIXEL_WIDTH, 16, black, yellow, white, dyellow);	btn = newButton(0, 0, 0, 0, 0, 0, " File ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, fileButtonCB);	addButton(menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Edit ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, editButtonCB);	addButton(menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Configure ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, configButtonCB);	addButton(menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Help ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpButtonCB);	addButton(menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " About ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutButtonCB);	addButton(menu, btn, FALSE);	showWidget(menu);	options = newMenu(0, FONT_HEIGHT + 4, SCREEN_PIXEL_WIDTH, 16, black, green, lgreen, dgreen);	chkb = newCheckBox(0, 0, 0, 0, 0, 0, "Check box", &checkflag);	attachCallBack(chkb, SDL_MOUSEBUTTONDOWN, checkBoxCB);	addCheckBox(options, chkb, TRUE);	chkb = newCheckBox(0, 0, 0, 0, 0, 0, "Another check box", &anotherflag);	attachCallBack(chkb, SDL_MOUSEBUTTONDOWN, checkBoxCB);	addCheckBox(options, chkb, TRUE);	chkb = newCheckBox(0, 0, 0, 0, 0, 0, "Toggle me", &toggleflag);	attachCallBack(chkb, SDL_MOUSEBUTTONDOWN, checkBoxCB);	addCheckBox(options, chkb, TRUE);	showWidget(options);	filename = newMenu(0, FONT_HEIGHT * 2 + 8, SCREEN_PIXEL_WIDTH, 16, black, gray, lgray, black);	addLabel(filename, newLabel(0, 0, 0, 0, 0, 0, "File: "), FALSE);	filelabel = newLabel(0, 0, 0, 0, 0, 0, " No file selected, use File\xc4\x1aOpen to select a file ");	filelabel->w = SCREEN_PIXEL_WIDTH - 8 * FONT_WIDTH;	addLabel(filename, filelabel, FALSE);	showWidget(filename);	file_menu = newVMenu(2, FONT_HEIGHT + 4, 0, 0, black, lgray, white, gray);	btn = newButton(0, 0, 0, 0, 0, 0, " Open ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, openButtonCB);	addButton(file_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Save ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, saveButtonCB);	addButton(file_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Close ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, closeButtonCB);	addButton(file_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " Exit ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, exitButtonCB);	addButton(file_menu, btn, FALSE);	help_menu = newVMenu(30, 80, 0, 0, black, yellow, white, gray);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpMenuCB);	addButton(help_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, "         Help");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpMenuCB);	addButton(help_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpMenuCB);	addButton(help_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " No help menu available ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpMenuCB);	addButton(help_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, helpMenuCB);	addButton(help_menu, btn, FALSE);	about_menu = newVMenu(100, 110, 0, 0, black, cyan, dblue, blue);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, "Test of Simple Widget C Code for SDL");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, "    (C) 2004 by Jeffery L. Post ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	btn =  newButton(0, 0, 0, 0, 0, 0, "    GNU General Public License ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	btn = newButton(0, 0, 0, 0, 0, 0, " ");	attachCallBack(btn, SDL_MOUSEBUTTONDOWN, aboutMenuCB);	addButton(about_menu, btn, FALSE);	testAlert = newAlertBox(50, 40,		"       This is a example\n             of an\n           Alert box\nabcdefghijklmnopqrstuvwxyz0123456789");	attachCallBack(testAlert, SDL_MOUSEBUTTONDOWN, alertCB);	chr = (char) 0;	for (i=0; i<16; i++)	{		for(j=0; j<16; j++)		{			font_set[i][j] = chr;			chr++;		}		font_set[i][j] = '\0';	}	font_set[0][0] = (char) 255;	tbox_test = newTextBox(60, 120, 200, 150, black, white, lgray, gray);	attachCallBack(tbox_test, SDL_MOUSEBUTTONDOWN, tboxCB);	attachCallBack(tbox_test, SDL_KEYDOWN, textboxCB);	tbbtn1 = newButton(1, -(FONT_HEIGHT * 2 + 5), black, lgray, white, gray, "       Text Box Test");	tbbtn2 = newButton(1, -(FONT_HEIGHT + 2), black, lgray, white, gray, "  Use arrows or PGUP/PGDOWN");	addButton(tbox_test, tbbtn1, TRUE);	addButton(tbox_test, tbbtn2, TRUE);	attachCallBack(tbbtn1, SDL_MOUSEBUTTONDOWN, nullCallBack);	attachCallBack(tbbtn2, SDL_MOUSEBUTTONDOWN, nullCallBack);	addText(tbox_test, "This is a text box test.");	addText(tbox_test, "Long line for testing limit at right end of the text box");	addText(tbox_test, "");	addText(tbox_test, "one");	addText(tbox_test, "two");	addText(tbox_test, "three");	addText(tbox_test, "four");	addText(tbox_test, "five");	addText(tbox_test, "six");	addText(tbox_test, "seven");	addText(tbox_test, "eight");	addText(tbox_test, "nine");	addText(tbox_test, "ten");	addText(tbox_test, "");	addText(tbox_test, "Note that this text box has two");	addText(tbox_test, "seemingly useless labels");	addText(tbox_test, "attached at the top.");	addText(tbox_test, "");	addText(tbox_test, "There are no callbacks for");	addText(tbox_test, "these labels--they are there");	addText(tbox_test, "just to provide information to");	addText(tbox_test, "the user.");	addText(tbox_test, "");	addText(tbox_test, "Labels may be attached to");	addText(tbox_test, "widgets either inside the");	addText(tbox_test, "widget or, as in this case,");	addText(tbox_test, "outside the widget.");	addText(tbox_test, "");	addText(tbox_test, "Buttons may also be attached");	addText(tbox_test, "outside the parent widget.");	addText(tbox_test, "");	addText(tbox_test, "Font set:");	addText(tbox_test, "");	for (i=0; i<16; i++)		addText(tbox_test, (char *) &font_set[i][0]);	addText(tbox_test, "");	addText(tbox_test, "Last line in this text box.");	tbbtn1->w = tbox_test->w;	tbbtn2->w = tbox_test->w;	ibox_test = newInputBox(40, 40, 150, 35, black, white, lgray, gray);	attachCallBack(ibox_test, SDL_MOUSEBUTTONDOWN, iboxCB);	attachCallBack(ibox_test, SDL_KEYDOWN, iboxCB);	addLabel(ibox_test, newLabel(0, 0, black, gray, lgray, black, "File name"), TRUE);	addInput(ibox_test, (char *) &fileNameInput);	strcpy(lineone, "Four line input box.");	strcpy(linetwo, "Starting in insert mode.");	linethree[0] = '\0';	linefour[0] = '\0';	mibox_test = newInputBox(140, 300, 150, 50, black, white, lgray, gray);	attachCallBack(mibox_test, SDL_MOUSEBUTTONDOWN, miboxCB);	attachCallBack(mibox_test, SDL_KEYDOWN, miboxCB);	mibox_label = newLabel(0, -(FONT_HEIGHT + 2), black, gray, lgray, black, "MultiLine InputBox");	addLabel(mibox_test, mibox_label, TRUE);	addInput(mibox_test, (char *) &lineone);	addInput(mibox_test, (char *) &linetwo);	addInput(mibox_test, (char *) &linethree);	addInput(mibox_test, (char *) &linefour);	showWidget(mibox_label);	showWidget(mibox_test);	hSlider = newHSlider(380, 320, 150, 15, lgray, lgray, white, gray, 100, 10);	attachCallBack(hSlider, SDL_MOUSEBUTTONDOWN, hSliderCB);	attachCallBack(hSlider, SDL_MOUSEBUTTONUP, hSliderCB);	attachCallBack(hSlider, SDL_MOUSEMOTION, hSliderCB);	attachCallBack(hSlider, SLIDER_AUTO_CALLBACK, hSliderAutoCB);	showWidget(hSlider);	hSliderLabel = newLabel(380, 300, white, black, 0, 0, hSliderText);	showWidget(hSliderLabel);	vSlider = newVSlider(450, 100, 15, 150, lgray, lgray, white, gray, 100, 10);	attachCallBack(vSlider, SDL_MOUSEBUTTONDOWN, vSliderCB);	attachCallBack(vSlider, SDL_MOUSEBUTTONUP, vSliderCB);	attachCallBack(vSlider, SDL_MOUSEMOTION, vSliderCB);	attachCallBack(vSlider, SLIDER_AUTO_CALLBACK, vSliderAutoCB);	showWidget(vSlider);	vSliderLabel = newLabel(390, 80, white, black, 0, 0, vSliderText);	showWidget(vSliderLabel);	SDL_UpdateRect(screen, 0, 0, 0, 0);	while (!quit)	{		SDL_UpdateRect(screen, 0, 0, 0, 0);		if (!SDL_PollEvent(NULL))			SDL_WaitEvent(NULL);		while (SDL_PollEvent(&event))		{			switch (event.type)			{				case SDL_KEYDOWN:#ifdef DEBUG					if (event.key.keysym.sym == SDLK_F1)						dump_widget_list();					else#endif					processCB(&event);					break;				case SDL_MOUSEBUTTONDOWN:				case SDL_MOUSEBUTTONUP:					processCB(&event);					break;				case SDL_MOUSEMOTION:					processCB(&event);					while (SDL_PollEvent(&event))					{						if (event.type != SDL_MOUSEMOTION)						{							SDL_PushEvent(&event);							break;						}					}					break;				case TIMER_EVENT:					processCB(&event);					break;				case SDL_QUIT:					quit = TRUE;					break;			}		}		if (!quit)			SDL_UpdateRect(screen, 0, 0, 0, 0);	}	widgetQuit();	SDL_Quit();	return(GOOD_EXIT);}								//  End of Main// end of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -