Sign in to follow this  
Oni Sephiroth

Drawing Sprites (bitmaps)

Recommended Posts

I've been playing around with some code from a textbook in an attempt to load a sprite. Well, the first time I ran the program my computer crashed (that was kind of interesting haha) and the second time, nothing but a black window happened. So anywho, here's my code that needs looking at.. My WinMain function..
Quote:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX windowClass; //window class HWND hwnd; //window handle MSG msg; //message bool done; //are we done with the main loop? //window class structure windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.style = CS_HREDRAW | CS_VREDRAW; windowClass.lpfnWndProc = WndProc; windowClass.cbClsExtra = 0; windowClass.cbWndExtra = 0; windowClass.hInstance = hInstance; windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION); windowClass.hCursor = LoadCursor( NULL, IDC_ARROW ); windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); windowClass.lpszMenuName = NULL; windowClass.lpszClassName = "My Class"; windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //register window class if ( !RegisterClassEx(&windowClass)) return 0; //class registered, create window hwnd = CreateWindowEx(NULL, "My Class", "Harvest Engine 1.0", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, //x and y coordinates 322, 300, //width and height NULL, NULL, hInstance, NULL ); //check if window creation failed if (!hwnd) return 0; ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); done = false; //init loop variable //main message loop while(!done) { PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE); if (msg.message == WM_QUIT) //did we get a quit message? { done = true; //exit loop } else { //clear screan and depth buffer glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glRasterPos2i(10,10); glDrawPixels(100,100,GL_RGB,GL_UNSIGNED_BYTE,Link.Sprite_Sheet[0]); SwapBuffers( g_HDC ); TranslateMessage(&msg); DispatchMessage(&msg); } //end if } //end while return msg.wParam; }
GLMain.h
Quote:
#ifndef __GLMAIN_H__ #define __GLMAIN_H__ #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/glaux.h> #define BITMAP_ID 0x4D42 //loads up a bitmap. unsigned char * LoadBitmapFile(char * filename, BITMAPINFOHEADER *bitmapinfoheader); //writes a bitmap int WriteBitmapFile(char * filename, int width, int height, unsigned char * imageData); #endif
GLMain.cpp
Quote:
#include "GLMain.h" //loads up a bitmap. unsigned char * LoadBitmapFile(char * filename, BITMAPINFOHEADER *bitmapinfoheader) { FILE *fileptr; BITMAPFILEHEADER bitmapfileheader; unsigned char *bitmapImage; int imageIdx = 0; unsigned char tempRGB; fileptr = fopen(filename, "rb"); if (fileptr == NULL) return NULL; fread(&bitmapfileheader, sizeof(BITMAPFILEHEADER), 1, fileptr); if (bitmapfileheader.bfType != BITMAP_ID) { fclose(fileptr); return NULL; } fread(bitmapinfoheader, sizeof(BITMAPINFOHEADER), 1, fileptr); fseek(fileptr, bitmapfileheader.bfOffBits, SEEK_SET); bitmapImage = (unsigned char*)malloc(bitmapinfoheader->biSize); if (!bitmapImage) { free(bitmapImage); fclose(fileptr); return NULL; } fread(bitmapImage, 1,bitmapinfoheader->biSize, fileptr); if (bitmapImage ==NULL) { fclose(fileptr); return NULL; } for (imageIdx = 0; imageIdx < bitmapinfoheader->biSize; imageIdx+=3) { tempRGB = bitmapImage[imageIdx]; bitmapImage[imageIdx] = bitmapImage[imageIdx + 2]; bitmapImage[imageIdx + 2] = tempRGB; } fclose(fileptr); return bitmapImage; } int WriteBitmapFile(char * filename, int width, int height, unsigned char * imageData) { FILE *fileptr; BITMAPFILEHEADER bitmapFileHeader; BITMAPINFOHEADER bitmapInfoHeader; int imageIdx; unsigned char tempRGB; fileptr = fopen(filename, "wb"); if (!fileptr) return 0; bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER); bitmapFileHeader.bfType = 0x4D42; bitmapFileHeader.bfReserved1 = 0; bitmapFileHeader.bfReserved2 = 0; bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bitmapInfoHeader.biPlanes = 1; bitmapInfoHeader.biBitCount = 24; bitmapInfoHeader.biCompression = BI_RGB; bitmapInfoHeader.biSizeImage = width * abs(height) * 3; bitmapInfoHeader.biXPelsPerMeter = 0; bitmapInfoHeader.biYPelsPerMeter = 0; bitmapInfoHeader.biClrUsed = 0; bitmapInfoHeader.biBitCount = 0; bitmapInfoHeader.biWidth = width; bitmapInfoHeader.biHeight = height; for (imageIdx = 0; imageIdx < bitmapInfoHeader.biSizeImage; imageIdx+=3) { tempRGB = imageData[imageIdx]; imageData[imageIdx] = imageData[imageIdx + 2]; imageData[imageIdx + 2] = tempRGB; } fwrite(&bitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), fileptr); fwrite(&bitmapInfoHeader, 1, sizeof(BITMAPINFOHEADER), fileptr); fwrite(imageData, 1, bitmapInfoHeader.biSizeImage, fileptr); fclose(fileptr); return 1; }
Player_Main.h
Quote:
#include "GLMain.h" #pragma once class CBase_Player { public: CBase_Player(); //main stuff BITMAPINFOHEADER bitmapinfoheader; //used to load sprites unsigned char * Main_Sprite; //main sprite unsigned char * Sprite_Sheet[2]; //Array of sprite sheets. Add an index value to add another sheet. /* 0 = Standing 1 = Walking */ };
Player_Main.cpp
Quote:
#include "Player_Main.h" CBase_Player::CBase_Player() { //Load up sprite sheets Sprite_Sheet[0] = LoadBitmapFile("Sprites\Link\Link0.bmp",&bitmapinfoheader); //Standing }

Share this post


Link to post
Share on other sites
Sorry, I haven't really looked at the code, but let me give you this advice:

Consider using SDL and an image library like Corona. SDL will set up a window and OpenGL viewport in like 10 lines of code, instead of around 100 in your script.

Corona is a lightweight image library, and is very easy to handle.

Both give you a major bonus: small and maintainable code. That way bug tracking is way easier then with your script now ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this