code help

Started by
3 comments, last by caffeineaddict 23 years, 3 months ago
I have been making a tetris clone for some time now and have just now gotten interested in it again, I have run into a snag though, well first of all i have made a 4x4 array for each piece like this int piece1 [4][4]= {{0,0,0,0}, {0,0,0,0}, {0,1,1,1}, {0,0,1,0}}; then do the same for all the other pieces, then i made a board array that is 20x10 anyway i was wondering how to draw a bitmap in each space that is occupied by a 1, if you would like more info please specify about what and i will try to give more info but please help if you can BTW i am using the windows GDI. Edited by - caffeineaddict on January 16, 2001 2:04:22 AM
Advertisement
I can only see two way to do this (from the info you have given),

1. Each 1 represents a small square bitmap, say 10x10 pixels. To display the whole shape, just render the small bitmaps according to their position in the array.

2. I haven''t played tetris for years but I dont think there are that many shapes in it. You might be better off making a bitmap for each shape and then just rotate that bitmap when needed.

Hope that helps
As I understand correctly, you don''t have to have a bitmap for each space that has value of 1, because this game is usually made up by a bunch of little squares. Every method should provide a way to draw a simple rectangle. for example, DirectX supports a Rectangle(RECT) function, and winSDK has this function too.
in the gdi version i made i did the same thing as you--> store the active piece and its orientation in a 4x4 grid.

each frame i would display the grid going x = 0..3,y = 0..3 and drawing a block (using a custom function DrawBlock() that made a bunch of gdi calls) if the grid[y][x] was occupied.

this is horribly slow, but it works.

later what i did was to change DrawBlock from calling like 6 gdi calls to calling one bitmap call.

(im'' not sure if you know) but here''s how i draw a bitmap using gdi:

(problem is that there''s global data needed... i eventually wrapped it in a c++ class.

basically what you do is save it in the .rc file, extract it from the .rc, save it to a HBITMAP to draw it.

1) draw ur bitmap and save it in the same directory as your .rc file.

// self explanatory

2) add this to your .rc file

ID_BM_image_tag BITMAP DISCARDABLE "filename"

character for character except that image_tag is the MACRO identifier you want to use to extract your bitmap, and filename is the name of the file (put it in the quotes)

// this will embed the bitmap into your exe, so you won''t have to externally load .bmp''s at runtime( which people can mess w/ or delete )

4) add your ID_BM_image_tag to your RESOURCE.h file, and give it a unique identifer..

#define IDBM_image_tag 110

// this will make the embedding in the rc accessible to the rest of te program

5) include "resource.h" in your main file

// duh

6) create a global HBITMAP ImageName, preforably named something akin to what the bitmap represents.

// this will be the pointer to the bitmap

7) during WinMain( Before calling createwindow ), do this:

g_hbmImageName = LoadImage( hInstance,MAKEINTRESOURCE( IDBM_image_tag ) );

// this basically points the HBITMAP to the right place


ok, now you know where it is...
in order to actually draw it, you have to give it its own hDC... this is so messy, but it''s what you gotta do...


8) declare a global HDC hImageDC, during WM_CREATE, get the hWnd''s dc, create a compatible dc for the bitmap, and -for the bitmap''s dc- select the bitmap as the current object.

messy, ok, but here is the code.

// Find window''s HDC to create bmp
hDC = GetDC( hWnd );

// Create hdc''s for the bitmap
g_hDCBitmap = CreateCompatibleDC( hDC );

// Select the hDC''s current object to be the bitmap
SelectObject( g_hDCBitmap,g_hbmBitmap );

// Clean up
ReleaseDC( hWnd,hDC );

9) to actually draw the bmp, you have to supply the window''s hdc, the bitmaps''s hdc, x and y to blit, and the width and height of the bmp, and call BitBlt:

BitBlt( hWindowDC,x,y,bmpWidth,bmpHeight,g_hbmBitmap,0,0,SRCCOPY );

it''s up to you to keep track of the bitmap''s width and height.

remember that the HBITMAP is only kinda like a pointer to the bitmap... to actually get the bitmap do this:

if you want to find this stuff out:
BITMAP bm;
GetObject( g_hbmBitmap,sizeof( BITMAP ),&bm );
bmpWidth = bm.bmWidth;
bmpHeight = bm.bmHeight;

but i''d suggest having bmpWidth and height also as globals, and only do this during wm_create...

hope this answered your how to draw a bitmap part.

wow, 9 steps... i''m glad i''m done w/ gdi (on to ogl!)




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have no name that you may call me. I am merely Succinct.
~Succinct Demos Online~

-(Drop me a line here n''at)-

-- Succinct(Don't listen to me)
Thanks everyone for replying, I will try what succinct said and hopefully get it up and running today.

This topic is closed to new replies.

Advertisement