• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PlayswithSquirrels

Allegro problems with tiles

2 posts in this topic

I'm relatively new at programming and am I'm making a 16-puzzle on allegro, and got a little stuck as to the tiles. I got as far as creating the structure, but as for the fitting the matrix in the loop I'm stumped. My goal is to break the image into 16 separate squares, with 1bit of distance between each, so I can see each of them defined. This is what I have so far.

#include <allegro.h>


void init();
void deinit();


typedef struct{
	BITMAP *piece;  //this would be a piece of the picture (1 of the 16 squares)
	int position;
}Block;

int main() {
	init();
	
	BITMAP *buffer;
	BITMAP *my_pic;
	PALETTE palette;
	clear_to_color(screen, makecol(255, 255, 255));

	buffer = create_bitmap(404,404);
	my_pic = load_bitmap("chihuahua2.bmp", palette);
		
	
	if(my_pic==NULL)
	{
		set_gfx_mode(GFX_TEXT, 0,0,0,0);
		allegro_message("Can't load chihuahua.bmp");
		return 1;
	}

	while (!key[KEY_ESC]) {
		blit(my_pic, screen, 0,0,106,48,404,404);
		show_mouse(my_pic);
		int i, t;
		for (i = 0; i <= 640; i++)
			for(t = 0; t <= 480; t++)
	//stuck on this area, don't know where to go from here			
		blit(my_pic, buffer, 0, 0, i, t, SCREEN_W, SCREEN_H);
	}
	destroy_bitmap(my_pic);
	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
}

void deinit() {
	clear_keybuf();
}

Any and all feedback is welcome.

Edited by PlayswithSquirrels
0

Share this post


Link to post
Share on other sites

Let's take a look at the loop you have so far:
 



while (!key[KEY_ESC]) {
		blit(my_pic, screen, 0,0,106,48,404,404);
		show_mouse(my_pic);
		int i, t;
		for (i = 0; i <= 640; i++)
			for(t = 0; t <= 480; t++)
	//stuck on this area, don't know where to go from here
		blit(my_pic, buffer, 0, 0, i, t, SCREEN_W, SCREEN_H);
	}

 
This code is going to keep running continually, so ask yourself "do I want to keep drawing my original image to the buffer over and over again, and why?"  I don't think you do.
 
Then you have two for loops that cause your image to be redrawn 307200 times, at various positions, a great many of which are outside of the buffer you're drawing them to. Again, this is not something you want to be doing.
 
So what do you want to be doing?  Well, you want to divide one image into 16 blocks and you've already created a struct to put them in, so lets start by creating an array of these structs with 16 members:
 



Block blocks[16];

 
The easiest way to get the 16 pieces is to do two loops to divide the image by four along the width and height. We can then create sub-bitmaps of the original image like so:
 



	int x, y, n = 0, block_w = my_pic->w / 4, block_h = my_pic->h / 4;

	for (y = 0; y < 4; y++) {

            for (x = 0; x < 4; x++) {

                blocks[n].piece = create_sub_bitmap(my_pic, x * block_w, y * block_h, block_w, block_h);
                blocks[n].position = n;
                n++;
            }
	}

For now, I've left the position numbers sequential. You can randomize them yourself later to mix up the pieces.

Whenever you want to draw your pieces in the positions they're in you can do this (this example draws them to the top-left corner of the buffer):



for (y = 0; y < 4; y++) {

    for (x = 0; x < 4; x++) 
        blit(blocks[y * 4 + x].piece, buffer, 0, 0, x * block_w, y * block_h, block_w, block_h);

}

I would have two buffers myself, one for the whole screen, and one for the play area. I would then blit all the pieces to the play area, from the top left, then I would blit this to your screen buffer. This way if you want to change where the play area is on your screen at a later point, there will be less code to change.

The following code puts all this together and allows to load your image, divide it into the 16 parts and then draw the 16 pieces. I'll let you take it form here to decide what to do next. If you need help after giving it a go yourself, feel free to ask.



#include <allegro.h>


void init();
void deinit();


typedef struct{
	BITMAP *piece;  //this would be a piece of the picture (1 of the 16 squares)
	int position;
}Block;

int main() {
	init();

	BITMAP *buffer, *play_buffer, *my_pic;
	PALETTE palette;
	clear_to_color(screen, makecol(255, 255, 255));

	my_pic = load_bitmap("chihuahua2.bmp", palette);
	buffer = create_bitmap(SCREEN_W, SCREEN_H);
        play_buffer = create_bitmap(my_pic->w, my_pic->h);



	if(my_pic==NULL)
	{
		set_gfx_mode(GFX_TEXT, 0,0,0,0);
		allegro_message("Can't load chihuahua.bmp");
		return 1;
	}

	Block blocks[16];

	int x, y, n = 0, block_w = my_pic->w / 4, block_h = my_pic->h / 4;

	for (y = 0; y < 4; y++) {

            for (x = 0; x < 4; x++) {

                blocks[n].piece = create_sub_bitmap(my_pic, x * block_w, y * block_h, block_w, block_h);
                blocks[n].position = n;
                n++;
            }
	}

	for (y = 0; y < 4; y++) {

            for (x = 0; x < 4; x++) 
                blit(blocks[y * 4 + x].piece, play_buffer, 0, 0, x * block_w, y * block_h, block_w, block_h);

	}

        // blit the play area bufer to the middle of the screen buffer
        blit(play_buffer, buffer, 0, 0, (buffer->w - play_buffer->w) / 2, (buffer->h - play_buffer->h) / 2, play_buffer->w, play_buffer->h);
        // then blit the screen buffer to the actual screen
        blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
        readkey();

        for (n = 0; n <16; n++)
            destroy_bitmap(blocks[n].piece);
	destroy_bitmap(my_pic);
	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
}

void deinit() {
	clear_keybuf();
}
Edited by LennyLen
1

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  
Followers 0