LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch ( msg ) { case WM_CREATE: { MakeSurface( hwnd ); } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint( hwnd, &ps ); // Draw pixels to window when window needs repainting BitBlt( hdc, 0, 0, mW, mH, hdcMem, 0, 0, SRCCOPY ); EndPaint( hwnd, &ps ); } break; case WM_CLOSE: { DestroyWindow( hwnd ); } break; case WM_DESTROY: { TerminateThread( hTickThread, 0 ); PostQuitMessage( 0 ); } break; default: return DefWindowProc( hwnd, msg, wParam, lParam ); } return 0; }
char LoadFont(const char *file) { char fullPath[strlen(DATA_PATH) + strlen(FONT_PATH) + strlen(file) + 1]; sprintf(fullPath, "%s%s%s", DATA_PATH, FONT_PATH, file); #ifdef DEBUG printf("Font path = %s\n", fullPath); #endif // The surface holding the big long list of characters SDL_Surface *fontSurf = FillSurface(fullPath, 0); if (fontSurf == NULL) { return 1; } // For holding the rgb values for pre-rendering differently-colored // letters SDL_Color palette[256]; uint r, g, b; // For finding the left and right side of each character (to determine // width) int leftSide; int rightSide; uint i = 0; uint transColor = SDL_MapRGB(fontSurf->format, 0xff, 0x00, 0xff); int sourceOffset = 0; for (int y = 0; y < fontSurf->h; y += FONT_H) { // Lock the surface (for subsequent GetPixel calls) SDL_LockSurface(fontSurf); /*** Determine the letter's width ***/ // Find left side leftSide = -1; for (int x2 = 0; x2 < FONT_W; x2++) { for (int y2 = y; y2 < y + FONT_H; y2++) { if (GetPixel(fontSurf, x2, y2) != transColor) { leftSide = x2; break; } } if (leftSide != -1) break; } // Find right side rightSide = -1; for (int x2 = FONT_W - 1; x2 > 0; x2--) { for (int y2 = y; y2 < y + FONT_H; y2++) { if (GetPixel(fontSurf, x2, y2) != transColor) { rightSide = x2; break; } } if (rightSide != -1) break; } font[i].w = (rightSide - leftSide) + 1; SDL_UnlockSurface(fontSurf); // Blit this character and pre-render different color versions // of it for (uint j = 0; j < NUM_FONT_COLORS; j++) { // Prepare a surface for this color of the character font[i].surf[j] = MakeSurface(FONT_W, FONT_H); // Blit the temporary fontSurf onto this letter's // surface ApplySurface(-leftSide, sourceOffset, fontSurf, font[i].surf[j]); // Get the rgb values for this color switch (j) { case 0: // Shadow r = 0; g = 0; b = 0; break; case 1: // Normal text r = 220; g = 220; b = 220; break; case 2: // Highlighted text r = 251; g = 177; b = 17; break; case 3: // Title r = 255; g = 255; b = 255; } // Set the palette of the surface for (uint k = 0; k < 256; k++) { palette[k].r = static_cast<Uint8>(r); palette[k].g = static_cast<Uint8>(g); palette[k].b = static_cast<Uint8>(b); } // Change the palette of the surface SDL_SetPalette(font[i].surf[j], SDL_LOGPAL, palette, 0, 256); } i++; if (i > FONT_ARRAY_SIZE - 1) break; sourceOffset -= FONT_H; // Move the big tall bmp up } SDL_FreeSurface(fontSurf); return 0; }