loading and displaying images

Questions regarding game mechanics and graphic programming should go here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

loading and displaying images

Postby candc32 » Tue Mar 14, 2006 10:44 pm

what library do I need to load .png's and .bmp's?
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby taymo2020 » Tue Mar 14, 2006 11:02 pm

SDL alone will do BMP's and SDL_image will load PNG's along with a couple more. Althought they all load into SDL's SDL_Surface structure that might be unusable in your situation.
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Postby Samuel » Wed Mar 15, 2006 4:23 am

For Bitmaps, you could use the windows function LoadImage(); or you could use the glaux library. I recommend LoadImage() because glaux has memory leaks.
But apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, the fresh-water system, and public health, what have the Romans ever done for us?
User avatar
Samuel
 
Posts: 676
Joined: Wed Apr 06, 2005 7:43 pm
Location: Australia

Postby candc32 » Wed Mar 15, 2006 3:29 pm

Samuel wrote:For Bitmaps, you could use the windows function LoadImage(); or you could use the glaux library. I recommend LoadImage() because glaux has memory leaks.

so I do this?
Code: Select all
#include <windows>
using namespace std;

int main()
{
    loadimage("test.bmp");
}
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby tomcant » Wed Mar 15, 2006 3:42 pm

candc32 wrote:
Samuel wrote:For Bitmaps, you could use the windows function LoadImage(); or you could use the glaux library. I recommend LoadImage() because glaux has memory leaks.

so I do this?
Code: Select all
#include <windows>
using namespace std;

int main()
{
    loadimage("test.bmp");
}

:shock: Heck no! Nothing is that simple with the WinAPI.

I put something together a while back that loads/displays more than just bitmap file formats. The following code loads and displays the file "pic.bmp" from the same directory as the executable. You can display most common file formats (gif, etc.), by simply changing the `const char* picture_file="...";' line in WndProc().

main.cpp
[syntax="cpp"]#include "picture.h"

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
static LPPICTURE pic=0;
static HDC hdc;
static PAINTSTRUCT ps;
static const char* picture_file="pic.bmp";

switch(msg){
case WM_CREATE:
pic=LoadPictureFile(picture_file);
break;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);

if(pic){
long hmWidth,hmHeight;
pic->get_Width(&hmWidth);
pic->get_Height(&hmHeight);

int nWidth=MulDiv(hmWidth,GetDeviceCaps(hdc,LOGPIXELSX),2540),
nHeight=MulDiv(hmHeight,GetDeviceCaps(hdc,LOGPIXELSY),2540);

RECT rc;
GetClientRect(hwnd,&rc);

pic->Render(hdc,0,0,nWidth,nHeight,0,hmHeight,hmWidth,-hmHeight,&rc);
}

EndPaint(hwnd,&ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd, msg, wParam, lParam);
} return 0;
}

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR args,int nShow){
MSG msg;
WNDCLASS wc={0};
wc.lpszClassName="Win32App";
wc.hInstance=hInst;
wc.hbrBackground=GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc=WndProc;
wc.hCursor=LoadCursor(0,IDC_ARROW);

RegisterClass(&wc);

CreateWindow(wc.lpszClassName,"Image Loader",WS_OVERLAPPEDWINDOW|
WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,400,400,0,0,hInst,0);

while(GetMessage(&msg,0,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}[/syntax]

picture.h
[syntax="cpp"]#ifndef _PICTURE_H_
#define _PICTURE_H_

#include <windows.h>
#include <olectl.h>
#include <crtdbg.h>

LPPICTURE LoadPictureFile(LPCTSTR);

#endif[/syntax]

picture.cpp
[syntax="cpp"]#include "picture.h"

LPPICTURE LoadPictureFile(LPCTSTR szFile){
HANDLE hFile=CreateFile(szFile,GENERIC_READ,0,0,OPEN_EXISTING,0,0);

DWORD dwFileSize=GetFileSize(hFile,0);

LPVOID pvData=0;
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);

pvData=GlobalLock(hGlobal);

DWORD dwBytesRead=0;
BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,0);
GlobalUnlock(hGlobal);
CloseHandle(hFile);

LPSTREAM pstm=0;
HRESULT hr=CreateStreamOnHGlobal(hGlobal,1,&pstm);

LPPICTURE pic;
hr=OleLoadPicture(pstm,dwFileSize,0,IID_IPicture,(LPVOID*)&pic);
pstm->Release();

return pic;
}[/syntax]

I havn't tested this for a while, but I think it worked the last time I checked. If you have any problems compiling the code, then post back here. Good luck!
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby candc32 » Wed Mar 15, 2006 8:55 pm

it said no file called picture.h
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby TheKidder » Wed Mar 15, 2006 9:13 pm

Make sure the file called picture.h (the one above picture.cpp in tomcant's post) is in the same directory as picture.h and main.cpp
w00t!
User avatar
TheKidder
 
Posts: 525
Joined: Mon Nov 24, 2003 7:57 pm
Location: MI

Postby candc32 » Wed Mar 15, 2006 10:58 pm

now its saying no file called crtdbg.h
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby taymo2020 » Wed Mar 15, 2006 11:49 pm

Just curious, what API are you using for graphics?
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Postby candc32 » Thu Mar 16, 2006 12:14 am

whats API stand for?
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby MXP » Thu Mar 16, 2006 12:54 am

Application Programming Interface

Taylor, it's OLE (Object Linking and Embedding... more or less COM). His method is a bit more powerful than LoadImage() but also worlds more complicated. I would personally just use LoadImage() and store everything as bitmaps. If I wanted PNGs then I'd get libpng.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby candc32 » Thu Mar 16, 2006 11:04 am

well can some one give me an example of loadiamge()
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Postby MXP » Thu Mar 16, 2006 11:17 am

This uses LoadBitmap() instead, but it's probably going to be more to you. You can replace any call to LoadBitmap() in that tutorial with a call to LoadImage().
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby Dante Shamest » Thu Mar 16, 2006 11:19 am

LoadImage is a Windows API function. Unless you're planning to draw a bitmap onto a window (and assuming you know how to create a window), it's probably not suitable for your needs.

What do you wish to display the bitmap on? A console window? An overlapped window? Or do you just wish to get information about the bitmap?

The following code will load a bitmap named "bitmap.bmp" in the same directory as the program and print the bitmap's width and height.

[syntax="cpp"]#include <windows.h>
#include <cstdio>

int main()
{
// Load the bitmap file, bitmap.bmp and get a handle to it.
HBITMAP hBitmap = (HBITMAP) LoadImage( GetModuleHandle(NULL),
"bitmap.bmp",
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE );

// Get information about the bitmap.
BITMAP bitmap = { 0 };
GetObject( hBitmap, sizeof(BITMAP), &bitmap ) ;

// Print information about the bitmap.
printf( "Bitmap width = %d\n", bitmap.bmWidth ) ;
printf( "Bitmap height = %d\n", bitmap.bmHeight ) ;

// Prevent windows console from closing immediately.
getchar() ;

return 0 ;
}[/syntax]

If you want to learn windows programming, check out the sticky in the windows programming forum. However you should already have a good grasp of C or C++.
User avatar
Dante Shamest
Moderator
 
Posts: 3131
Joined: Wed Oct 22, 2003 10:29 pm
Location: Malaysia

Postby candc32 » Thu Mar 16, 2006 4:10 pm

ok what I want is to make a game like a little adventure zelda thing
Image
Image
candc32
 
Posts: 10
Joined: Fri Mar 10, 2006 6:45 pm

Next

Return to Games and Graphics

Who is online

Users browsing this forum: No registered users and 2 guests