Beispiel #1
0
static int
ifscan(void)
{
	struct ifreq ifr[NUMIFS];
	struct ifconf ifc;
	int sd, i, j=0, ifnum, addr;

	/* clear old interfaces */
	clear();

	/* create a socket where we can use ioctl to retrieve interface info */
	sd = socket(PF_INET, SOCK_DGRAM, 0);
	if (sd <= 0) {
		wrlog("Failed: socket(PF_INET, SOCK_DGRAM, 0)\n");
		return -1;
	}

	/* set pointer and maximum space (???) */
	ifc.ifc_len = sizeof(ifr);
	ifc.ifc_ifcu.ifcu_buf = (caddr_t) ifr;

	/* get interface info from the socket created above */
	if (ioctl(sd, SIOCGIFCONF, &ifc) != 0) {
		wrlog("Failed: ioctl(sd, SIOCGIFCONF, &ifc)\n");
		close(sd);
		return -1;
	}
	
	/* get number of found interfaces */
	ifnum = ifc.ifc_len / sizeof(struct ifreq);

	for (i = 0; i < ifnum; ++i) {
		if (ifr[i].ifr_addr.sa_family != AF_INET)
			continue;

		if (!ioctl(sd, SIOCGIFADDR, &ifr[i]) && strcmp(ifr[i].ifr_name, "lo")) {
			/* create a new NetworkInterface to assign information to it */
			netifs[j] = malloc(sizeof(NetworkInterface));

			/* name */
			snprintf(netifs[j]->name, BUFLEN-1, "%s", ifr[i].ifr_name);

			/* address */
			addr = ((struct sockaddr_in *) (&ifr[i].ifr_addr))->sin_addr.s_addr;
			snprintf(netifs[j]->ip, BUFLEN-1, "%d.%d.%d.%d",
					 addr      & 0xFF, (addr>> 8) & 0xFF,
					(addr>>16) & 0xFF, (addr>>24) & 0xFF);

			/* state (UP/DOWN) */
			netifs[j]->active = (!ioctl(sd, SIOCGIFFLAGS, &ifr[i]) &&
					ifr[i].ifr_flags | IFF_UP);
			j++;
		}
	}
Beispiel #2
0
Datei: log.c Projekt: cmr/iiag
void wrlog(const char * fmt, ...) {
	static FILE * logf = NULL;

	char * str;
	time_t tm;
	va_list vl;

	if (logf == NULL) {
		logf = fopen(log_file, "a");
		if (logf == NULL) return;
		wrlog("Opened log file");
	}

	time(&tm);
	str = ctime(&tm);
	str[strlen(str) - 1] = 0;
	fprintf(logf, "%s | ", str);

	va_start(vl, fmt);
	vfprintf(logf, fmt, vl);
	va_end(vl);

	fputc('\n', logf);
	fflush(logf);
}
Beispiel #3
0
/**
Parses the Sprite Sheet definition file and reads the specified keywords.
This could be broken out in other, more robust token parsing code.
*/
bool Sprite::ParseDefinition(const std::string &fontfile)
{
	std::string Line;
	std::string Read, Key, Value;
	std::size_t i;

	wrlog("Sprite Def here is %s", fontfile.c_str());

	std::ifstream Stream(fontfile.c_str());
	//Temporary Sprite 
	SpriteDescriptor C;

	while (!Stream.eof())
	{
		std::stringstream LineStream;
		std::getline(Stream, Line);
		LineStream << Line;

		//read the line's type
		LineStream >> Read;

		if (Read == "<TextureAtlas")
		{
			//this holds common data
			while (!LineStream.eof())
			{
				std::stringstream Converter;
				LineStream >> Read;
				i = Read.find('=');
				Key = Read.substr(0, i);
				Value = Read.substr(i + 1);

				if (Value.front() == '"') {
					Value.erase(0, 1); // erase the first character
					Value.erase(Value.size() - 1); // erase the last character
					//To remove the double quotes.
				}

				//assign the correct value
				Converter << Value;
				if (Key == "height")
				{
					Converter >> sheetHeight;
				}
				else if (Key == "width")
				{
					Converter >> sheetWidth;
				}
				else if (Key == "imagePath")
Beispiel #4
0
Datei: log.c Projekt: cmr/iiag
void end_timer(const char * name) {
	clock_t cnt = clock() - sclock;
	double sec = (double)cnt / (double)CLOCKS_PER_SEC;
	wrlog("Timer %s: %g seconds (%d clocks)", name, sec, cnt);
}
Beispiel #5
0
static int
update(void)
{
	FILE *f;
	int i, c=0;

	dy[0] = 0;
	for (i = 0; i < NUMIFS; ++i) {
		/* check if network interface */
		if (!netifs[i] || (!netifs[i]->active && !show_inactive_if))
			continue;

		/* separator if there's been an interface before */
		if (c > 0)
			snprintf(dy+strlen(dy), DISPLEN-strlen(dy), "  ");

		/* quality (if wlan) */
		if (!strcmp(netifs[i]->name, "wlan0")) {
			f = fopen("/proc/net/wireless", "r");
			if (f == NULL) {
				wrlog("Failed to open file: /proc/net/wireless\n");
				return -1;
			}
			fscanf(f, "%*[^\n]\n%*[^\n]\n%*s %*d %d.%*s",
					&netifs[i]->quality);
			if (ferror(f)) {
				fclose(f);
				wrlog("Failed to open file: /proc/net/wireless\n");
				fclose(f);
				return -1;
			}
			fclose(f);
			snprintf(dy+strlen(dy), DISPLEN-strlen(dy),
					"^fg(#%X)^i(%s/network_wifi_%d.xbm)^fg()",
					colour(netifs[i]->quality), path_icons,
					netifs[i]->quality/20);
		}

		/* ethernet icon (if eth) */
		if (!strcmp(netifs[i]->name, "eth0")) {
			snprintf(dy+strlen(dy), DISPLEN-strlen(dy),
					"^i(%s/network_eth.xbm)", path_icons);
		}

		/* VPN icon (if VPN) */
		if (!strcmp(netifs[i]->name, "tun0")) {
			snprintf(dy+strlen(dy), DISPLEN-strlen(dy),
					"^i(%s/network_tun.xbm)", path_icons);
		}

		/* IP address */
		snprintf(dy+strlen(dy), DISPLEN-strlen(dy),
				" ^fg(#%X)%s^fg()",
				netifs[i]->active ? colour_hl : colour_err, netifs[i]->ip);

		/* counter */
		c++;
	}

	if (c == 0)
		sprintf(dy, "no network");

	return 0;
}
Beispiel #6
0
// Simple, generic window init //
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int iCmdShow)
{
	WNDCLASS wc;

	MSG msg;
	bool Quit = FALSE;
	DWORD       dwExStyle;                      // Window Extended Style
	DWORD       dwStyle;                        // Window Style

	// register window class
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = L"BMFontGL";
	RegisterClass(&wc);

	dwExStyle = WS_EX_APPWINDOW;   // Window Extended Style    
	dwStyle = WS_OVERLAPPEDWINDOW | WS_THICKFRAME;                    // Windows Style
	RECT WindowRect;                                                 // Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left = (long)0;                                         // Set Left Value To 0
	WindowRect.right = (long)WinWidth;                                 // Set Right Value To Requested Width
	WindowRect.top = (long)0;                                          // Set Top Value To 0
	WindowRect.bottom = (long)WinHeight;                               // Set Bottom Value To Requested Height
	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);      // Adjust Window To True Requested Size


	// Create The Window
	if (!(hWnd = CreateWindowEx(dwExStyle,							// Extended Style For The Window
		L"BMFontGL",							// Class Name
		L"OpenGL BMFont Sample Implementation",						// Window Title
		dwStyle |							// Defined Window Style
		WS_CLIPSIBLINGS |					// Required Window Style
		WS_CLIPCHILDREN,					// Required Window Style
		CW_USEDEFAULT, 0,   				// Window Position
		WindowRect.right - WindowRect.left,	// Calculate Window Width
		WindowRect.bottom - WindowRect.top,	// Calculate Window Height
		NULL,								// No Parent Window
		NULL,								// No Menu
		hInstance,							// Instance
		NULL)))								// Dont Pass Anything To WM_CREATE
	{
		// Reset The Display
		MessageBox(NULL, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	//********** Program Initializations *************
	//Enable Logging
	LogOpen("glfonttest.log");
	// enable OpenGL for the window
	CreateGLContext();
	//Basic Window Init
	WRLOG("Starting Program");
	ShowWindow(hWnd, SW_SHOW);                                         // Show The Window
	SetForegroundWindow(hWnd);									    // Slightly Higher Priority
	SetFocus(hWnd);													// Sets Keyboard Focus To The Window
	ReSizeGLScene(WinWidth, WinHeight);										    // Set Up Our Perspective GL Screen
	//Get the Supported OpenGl Version
	CheckGLVersionSupport();
	//Fill in the Window Rect;
	GetClientRect(hWnd, &MyWindow);
	//Set the OpenGl View
	ViewOrtho(WinWidth, WinHeight);

	//Load and Initialize the Fonts
	wrlog("Starting to parse fonts.");

	Lucida = new BMFont(WinWidth, WinHeight);
	if (!Lucida->LoadFont("lucida.fnt"))
	{
		MessageBox(NULL, L"Error, font file not found, exiting", L"File Not Found", MB_ICONERROR | MB_OK);
		Quit = TRUE;
		//PostQuitMessage(-1);
	}
	LOG_DEBUG("Font Loaded Sucessfully");

	Snap = new BMFont(WinWidth, WinHeight);
	if (!Snap->LoadFont("snap.fnt"))
	{
		MessageBox(NULL, L"Error, font file not found, exiting", L"File Not Found", MB_ICONERROR | MB_OK);
		Quit = TRUE;
		//PostQuitMessage(-1);
	}
	LOG_DEBUG("Font Loaded Sucessfully");

	Times = new BMFont(WinWidth, WinHeight);
	if (!Times->LoadFont("times.fnt"))
	{
		MessageBox(NULL, L"Error, font file not found, exiting", L"File Not Found", MB_ICONERROR | MB_OK);
		Quit = TRUE;
		// PostQuitMessage(-1);
	}
	LOG_DEBUG("Font Loaded Sucessfully");

	// ********** Program Main Loop **********

	while (!Quit) {

		// check for messages
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

			// handle or dispatch messages
			if (msg.message == WM_QUIT) {
				Quit = TRUE;
			}
			else {
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}

		}
		else {

			// ********** OpenGL drawing code, very simple, just to show off the font. **********

			glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			glClear(GL_COLOR_BUFFER_BIT);

			// setup texture mapping
			glEnable(GL_TEXTURE_2D);

			glLoadIdentity();
			Times->SetAlign(BMFont::AlignCenter);
			Times->SetScale(1.5f);
			Times->SetColor(250, 251, 252, 255);
			Times->Print(0, 280, "A different font, centered. Kerning: To Ti");

			/*
			Lucida->setColor(250,250,55,254);
			Lucida->PrintCenter(240,"This is another font.");

			Snap->setColor(255,255,255,255);
			Snap->setScale(1.0f);
			Snap->Print(0, 0, "This is standard printing.");
			*/
			Snap->SetColor(RGB_WHITE);
			Snap->SetAngle(0);
			Snap->SetScale(2.0f);
			Snap->SetAlign(BMFont::AlignNear);
			Snap->Print(40.0, 180, "Scaling makes it Big!");

			Snap->SetColor(RGB_WHITE);
			Snap->SetScale(1.0f);
			Snap->SetAngle(90);
			Snap->Print(130.0, 100, "The next line is right here.");
			Snap->SetAngle(0);
			Snap->Print(130.0, 350, "Another long line here.");
			/*
			Snap->setScale(1.0f);
			Snap->Print(2, Snap->getHeight()*4, "Or it can make it smaller!");
			Snap->setScale(1.0f);
			Snap->setColor(25,155,255,255);
			Snap->PrintCenter(320, "Centered printing: To Ti");
		   */
			Times->Render();
			Snap->Render();
			GlSwap();
		}
	}

	// ********** Cleanup and exit gracefully. **********
	// shutdown OpenGL
	delete Lucida;
	delete Snap;
	delete Times;
	DeleteGLContext();
	LogClose();
	// destroy the window 
	DestroyWindow(hWnd);

	return (int)msg.wParam;
}
Beispiel #7
0
void SnapShot()
{
	unsigned char *bmpBuffer;
	time_t tim;
	struct tm tm;
	char buf[15];
	char temppath[80];

	int Width = 0;
	int Height = 0;
	int i = 0;

	RECT rect;

	GetClientRect(win_get_window(), &rect);
	Width = rect.right - rect.left;
	Height = rect.bottom - rect.top;

	memset(&temppath[0], 0, sizeof(temppath));

	//Get time/date stamp
	tim = time(0);
	tm = *localtime(&tim);
	strftime(buf, sizeof buf, "%Y%m%d%H%M%S", &tm);
	puts(buf);
	//////
	bmpBuffer = (unsigned char*)malloc(Width*Height * 4);

	if (!bmpBuffer) wrlog("Error creating buffer for snapshot"); return;

	glReadPixels((GLint)0, (GLint)0, (GLint)Width, (GLint)Height, GL_RGBA, GL_UNSIGNED_BYTE, bmpBuffer);

	//Flip Texture
	int width_in_bytes = Width * STBI_rgb_alpha;
	unsigned char *top = NULL;
	unsigned char *bottom = NULL;
	unsigned char temp = 0;
	int half_height = Height / 2;

	for (int row = 0; row < half_height; row++)
	{
		top = bmpBuffer + row * width_in_bytes;
		bottom = bmpBuffer + (Height - row - 1) * width_in_bytes;
		for (int col = 0; col < width_in_bytes; col++)
		{
			temp = *top;
			*top = *bottom;
			*bottom = temp;
			top++;
			bottom++;
		}
	}

	strcat(temppath, "SS");
	strcat(temppath, buf);
	strcat(temppath, ".png");
	strcat(temppath, "\0");
	LOG_DEBUG("Saving Snapshot: %s", temppath);

	stbi_write_png(temppath, Width, Height, 4, bmpBuffer, Width * 4);

	free(bmpBuffer);
}
Beispiel #8
0
//----------------------------------TEXTURE Handling----------------------------------
//This function has a lot more options in my main library
//This is just a minimum build.
TEX *LoadPNG(std::string filename) 
{
	GLuint tex = 0;
	int x = 0;
	int y = 0;
	int comp = 0;

	TEX *temptex = new (TEX);

	unsigned char *image_data = stbi_load(filename.c_str(), &x, &y, &comp, STBI_rgb_alpha);
	if (!image_data) {
		wrlog("ERROR: could not load %s\n", filename);
		return 0;
	}
	wrlog("Texture x is %d, y is %d, components %d", x, y, comp);
	// NPOT check
	if ((x & (x - 1)) != 0 || (y & (y - 1)) != 0) {
		wrlog("WARNING: texture %s is not power-of-2 dimensions\n", filename);
	}
	//Flip Texture
	int width_in_bytes = x * STBI_rgb_alpha;
	unsigned char *top = NULL;
	unsigned char *bottom = NULL;
	unsigned char temp = 0;
	int half_height = y / 2;

	for (int row = 0; row < half_height; row++) {
		top = image_data + row * width_in_bytes;
		bottom = image_data + (y - row - 1) * width_in_bytes;
		for (int col = 0; col < width_in_bytes; col++) {
			temp = *top;
			*top = *bottom;
			*bottom = temp;
			top++;
			bottom++;
		}
	}
	//Create Texture
	glGenTextures(1, &temptex->texid);
	glBindTexture(GL_TEXTURE_2D, temptex->texid);
	glTexImage2D(
		GL_TEXTURE_2D,
		0,
		GL_RGBA,
		x,
		y,
		0,
		GL_RGBA,
		GL_UNSIGNED_BYTE,
		image_data
	);

	//glGenerateMipmap(GL_TEXTURE_2D); //Only if using opengl 3+, next example...
	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	GLfloat max_aniso = 0.0f;
	glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_aniso);
	// set the maximum!
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso);

	stbi_image_free(image_data); //Now that we're done making a texture, free the image data
	
								 //set image data properties
	temptex->bpp = comp;
	temptex->height = x;
	temptex->width = y;
	
	return temptex;
}