Qlgame 2d Engine builds 2d game principle

Source: Internet
Author: User

Hello everyone, the 2d engine is based on OpenGL (ES) 3d technology, because it has many advantages over the previous DDraw!

1. Coordinate with floating-point number, can be used for curve movement

2. If the image is scaled and rotated in DDraw, it is based on pixel operation, it is very cost-performance, and 3d, it is easy to do the work!

3. It is based on rendering a panel, then pasting the stickers on the panel and then rotating and zooming the panel on the action!

4.2d animation is based on UV, you can put all the animation in a large map, and then constantly switch the UV implementation of the animation switch!

Today's working environment configuration:

Os:win7 64bit

tool:vs2012

There are two main contents: 1. Render square 2. Render Map

Since we are 2d games, we use the Glortho 2d view to set the view, with the following steps:

Detailed:

Glviewport () for window view settings such as 800,600

Glmatrixmode (gl_projection); Set Model view

Glortho Set 2d view,-400,400, indicating that the width is 800,-300,300, High is 600,-100 is near the cutting surface, 100 is the far cutting surface

Because it is in the 2d view, I want to play every pixel of the game window, occupy a unit, so good for the coordinates, very good calculation!

Glbegin begins to draw a geometric patch, which is not supported in Opengles Method!

GLVERTEX2F is the draw point, called glvertex2f four times, to draw a panel out

Glclear (Gl_color_buffer_bit);

Glviewport (0,0,800,600);

Glmatrixmode (gl_projection);

Glloadidentity ();

Glortho ( -400,400,-300,300,-100,100);

Glbegin (Gl_polygon);

Glfloat w=100;

Glfloat h=100;

GLVERTEX2F (-W,-H);

GLVERTEX2F (-W, h);

GLVERTEX2F (W, h);

GLVERTEX2F (W,-H);

Glend ();

Glflush ();

Start mapping below

The map uses the Open Source Library FreeImage3.17.0 library, which is: http://sourceforge.net/projects/freeimage/files/Source%20Distribution/

There is a class for OpenGL used in FreeImage3.17.0, called TextureManaged.h TextureManaged.cpp

Freeimage has 32-bit and 64-bit versions, here the version corresponds, not that your computer system is 32bit, or 64bit!

It means that your editor is 32-bit or 64-bit!

The same settings are in the Include directory, as well as in the link library, and there are additional dependencies!

The rendering image code is as follows:

#include "TextureManager.h"//Note this sentence should be above <glut.h>, otherwise it will be wrong
#include <glut.h>
Gluint Textures[1];
Show Callback function
void Renderscreen (void) {
Clear the entire window as the current cleanup color: pumpkin Orange
Glclear (Gl_color_buffer_bit);
Glviewport (0,0,800,600);
Glmatrixmode (gl_projection);
Glloadidentity ();
Glortho ( -400,400,-300,300,-100,100);
Texturemanager::inst ()->bindtexture (textures[0]);//bind map here
Glbegin (Gl_polygon);
Glfloat w=100;
Glfloat h=100;
GLTEXCOORD2D (0,0); glvertex2f (-W,-H);
GLTEXCOORD2D (0,1); glvertex2f (-W, h);
GLTEXCOORD2D (n); glvertex2f (W, h);
GLTEXCOORD2D (1,0); glvertex2f (W,-H);
Glend ();
Glflush ();
Glutswapbuffers ();
}

                 void Init ()
            {
                        Texturemanager::inst ()->loadtexture ("./1.jpg", textures[0]);//load Map
                       glenable (gl_texture_ 2D);//enable 2d textures
                       Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear);    //Linear filter
                       Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear);    //Linear filter

}

int main (int argc, char* argv[])
{

Initialize GLUT
Glutinit (&AMP;ARGC,ARGV);
Single buffer
Glutinitdisplaymode (glut_single| GLUT_RGBA);
Create a window with a window named Windowdemo
Glutcreatewindow ("Qlgameengine");
Set Window size
Glutreshapewindow (800,600);
Init ();
Setting the display callback function
Glutdisplayfunc (Renderscreen);
Glutmainloop ();
return 0;
}

Detailed

1.texturemanager::inst ()->loadtexture ("./1.jpg", textures[0]);//Load Map

2. Enable textures and use texture properties

Glenable (gl_texture_2d);//Enable 2d Textures
Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear); Linear filtering
Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear); Linear filtering

3. Binding Map

Texturemanager::inst ()->bindtexture (textures[0]);//bind map here

4. Setting vertices and Uvs for rendering maps

Glbegin (Gl_polygon);
Glfloat w=100;
Glfloat h=100;
GLTEXCOORD2D (0,0); glvertex2f (-W,-H);
GLTEXCOORD2D (0,1); glvertex2f (-W, h);
GLTEXCOORD2D (n); glvertex2f (W, h);
GLTEXCOORD2D (1,0); glvertex2f (W,-H);
Glend ();

Operation Result:

A cute little turtle, show out, hehe!

I have two problems to solve:

1. It is the color of the turtle discoloration, the original image is like this

2. I am in the debug directory, directly launched the generated EXE, you can see the small turtle pictures, but in the run directly on Vs do not see the picture, I set the working directory for

of Debug,

Have to know how to solve these two problems, please leave a message with me, sincerely want to contact friends!

Qlgame 2d Engine builds 2d game principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.