rgg platformer

posted in phil67rpg's Blog
Published September 15, 2019
Advertisement

well I have drawn animated sprite which can move back and  forth across the bottom of the screen just above some grass sprites. here is my code.


#include <freeglut.h>
#include <iostream>
#include<SOIL.h>

using namespace std;

GLuint texture[8];

float screen = 0.0f;
float move_sprite = 0.0f;

void update(int value)
{
	screen += 0.125f;
	if (screen >= 0.875f)
	{
		screen = 0.0f;
	}
	glutPostRedisplay();
	glutTimerFunc(500, update, 0);
}

void drawSprite()
{
	glEnable(GL_TEXTURE_2D);
	glPushMatrix();
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTranslatef(0.0f, -70.0f, 0.0f);
	glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + screen, 0.0f, 0.0f);

	glVertex3f(10.0f, -10.0f+ move_sprite, 0.0f);
	glTexCoord3f(0.125f + screen, 0.0f, 0.0f);

	glVertex3f(10.0f, 10.0f+ move_sprite, 0.0f);
	glTexCoord3f(0.125f + screen, 1.0f, 0.0f);

	glVertex3f(-10.0f, 10.0f+ move_sprite, 0.0f);
	glTexCoord3f(0.0f + screen, 1.0f, 0.0f);

	glVertex3f(-10.0f, -10.0f+ move_sprite, 0.0f);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
}

void drawBkgnd()
{
	glEnable(GL_TEXTURE_2D);
	glPushMatrix();
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glTranslatef(0.0f, -90.0f, 0.0f);
	glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(10.0f, -50.0f, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(10.0f, 50.0f, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(-10.0f, 50.0f, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(-10.0f, -50.0f, 0.0f);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
}

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_POWER_OF_TWO
	);
	return texture;
}

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

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	drawBkgnd();
	drawSprite();
	glPopMatrix();
	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat aspectRatio;
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
		glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void handleSpecialKeypress(int key, int, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		move_sprite++;
		if (move_sprite >= 125.0f)
		{
			move_sprite = 125.0f;
		}
		break;
	case GLUT_KEY_RIGHT:
		move_sprite--;
		if (move_sprite <= -125.0f)
		{
			move_sprite = -125.0f;
		}
		break;
	}
	glutPostRedisplay();
}

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(400, 300);
	glutInitWindowSize(800, 600);
	glutTimerFunc(500, update, 0);
	glutCreateWindow("Rpg");
	glutDisplayFunc(renderScene);
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(handleSpecialKeypress);
	init();
	glutMainLoop();
	return 0;
}

 

Previous Entry vetical side scroller
Next Entry plane game
2 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2661 views

plane game

3410 views

rgg platformer

2043 views

win 32 pong

2523 views

bug invaders

2190 views

c# book

2474 views

c# bug invaders

2131 views

c# console snake game

23812 views
Advertisement