Skip to main content
GameDev.net gamedev.net
🔒 Locked

animating sprites

Started by phil67rpg Aug 21, 2019 at 11:30 PM 42 replies 12.3k views
Original Post
phil67rpg
phil67rpg

I finally got soil to load a png file. my next step is to animate a sprite using a sprite sheet I am unsure of how to start. here is my code.


GLuint textureBrick[8]; 

GLuint loadTex(const char* texname)                                    
{
	/* load an image file directly as a new OpenGL texture */
	GLuint texture = SOIL_load_OGL_texture
	(
		texname,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
	);
	return texture;
}

void init()
{
	textureBrick[0] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
	glBegin(GL_QUADS);
	glTexCoord2i(0, 0);
	glVertex2i(10, 10);
	glTexCoord2i(1, 0);
	glVertex2i(10, -10);
	glTexCoord2i(1, 1);
	glVertex2i(-10, -10);
	glTexCoord2i(0, 1);
	glVertex2i(-10, 10);
	glEnd();
	glPopMatrix();
	glutSwapBuffers();
	glDisable(GL_TEXTURE_2D);
}


markypooch
markypooch

Hey Phil,

How I've done it before is to divide the number of sprites in a column, and row by 1.0


y_stride = 1.0f / number_of_sprites_in_column
x_stride = 1.0f / number_of_sprites_in_row

And define a "step" rate for my animation, that is, how often should I increment the current animation U/V values by the aforementioned strides.

With these u/v values, you can then plug them into your immediate mode code there with the spritesheet bound.

phil67rpg
phil67rpg

well I have done this before, but it has been awhile since I have done this. I think that I can use the glTexcoord2i function, thanks for all the help.

NubDevice
NubDevice

use the float version instead. The image coordinate space are all values between 0.0 and 1.0

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.
phil67rpg
phil67rpg

I have changed my code, but I still do not know how to animate sprites also I have got to draw two sprites on the screen.


void init()
{
	textureBrick[0] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
	textureBrick[1] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(100.0f, 10.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(100.0f, -10.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(80.0f, -10.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(80.0f, 10.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, textureBrick[1]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-100.0f, 10.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(-100.0f, -10.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(-80.0f, -10.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(-80.0f, 10.0f);
	glEnd();
	glPopMatrix();
	glutSwapBuffers();
	glDisable(GL_TEXTURE_2D);
}


markypooch
markypooch

Hey Phil,

ignoring your past record here so you don't feel like we are keeping tabs on you ;), however, you need to give us more then

Quote

I have changed my code, but I still do not know how to animate sprites also I have got to draw two sprites on the screen.

and then reposting your wall of code. That tells me very little on what exactly you are struggling with. Was it how I phrased it? Is there something I said that came off as ambiguous, or confusing? Let me know!

What I STRONGLY advise is to avoid copy/pasting code; hoping it works, and resting all your hopes on a forum to essentially crowd-build your application for you. You will not get far in any flavor of software development with that approach.

phil67rpg
phil67rpg

well I figured out how to animate a sprite but I don't know what game I should work on. also what is a good link for finding sprites?

Tom Sloper
Tom Sloper
5 hours ago, phil67rpg said:

I don't know what game I should work on.

Choose a very simple game. Remake Breakout or Space Invaders.

-- Tom Sloper    --      sloperama.com
phil67rpg
phil67rpg

does anyone remember 1942 I am going to make a game like space invaders only using planes and a vertical side scroller.

Prototype
Prototype

1942 is not a very simple game. Take something you can finish in a considerable timeframe, like couple of days or something. Add a title screen, options etc. Then polish it and move on to something slighty more advanced. See how long that takes.

Work with what you know already, constantly exploring new terrain doesn't get you anywhere. People might also be able to help you better if you stuck with one subject.

phil67rpg
phil67rpg

ok do you have any ideas for me?

Tom Sloper
Tom Sloper
1 hour ago, phil67rpg said:

ok do you have any ideas for me?

Make a simple game, like Breakout.

-- Tom Sloper    --      sloperama.com
Prototype
Prototype

Also, pick a graphics library. Messing around with raw OpenGL only makes things difficult. Perhaps SDL/SFML or something would suit your needs.

Steven Ford
Steven Ford

Seconding @Prototype's and @Tom Sloper's comments. If you start with making your end state game on day 1, realistically, you'll do a poorer job of it than if that game is your second or subsequent game.

by picking a simpler game, almost of any type you reduce the amount of stuff that's needed to learn before you can achieve something. Aiming to do 3D graphics on day 1 etc (not that you're intending to) is going to add frustrations. If you're using DX, then the DirectX Toolkit simplifies the presentation of 2D images etc. Once you've got something on screen, then it becomes so much more motivating to improve it!

Starting ideas for games:

* Mr E clones
* space invaders
* pacman
* Repton (maybe that as game #2 as well as showing my age!! :))

Personally, I came back into game making after ~20 years out and then decided to write my dream game. That was a mistake. It took a lot longer than it would have done than if I had followed the advice above as I ended up losing motivation due to the time it took to achieve something on screen.

Main thing though... enjoy it!

fleabay
fleabay
3 hours ago, Steven Ford said:

Mr E

What is Mr E?? The only thing I could find related to games were a couple of YouTubers that was using that as short for mystery (Mr E-Game, cringe).

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
Steven Ford
Steven Ford
3 minutes ago, fleabay said:

What is Mr E?? The only thing I could find related to games were a couple of YouTubers that was using that as short for mystery (Mr E-Game, cringe).

Hmmm Google doesn't appear to be much use here. I'll do some searching to see what I can find.

It was a 2D game where, if I remember correctly, you had to collect cherries and could drop apples on the end of the aliens chasing you

fleabay
fleabay
4 minutes ago, Steven Ford said:

It was a 2D game where, if I remember correctly, you had to collect cherries and could drop apples on the end of the aliens chasing you

Mr. Do, That's a hard game to program.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.
Steven Ford
Steven Ford

Turns out that it was called 'Mr EE' ?

YouTube

Just now, fleabay said:

Mr. Do, lol

hahahahaha - I guess this shows that we played the same game but on different machines ?

fleabay
fleabay
8 minutes ago, Steven Ford said:

I guess this shows that we played the same game but on different machines

Mr. EE was a shameless clone of Mr. Do. :) Get MAME and play the original.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.