How to use soil to display pictures in a VS2012 C + + Environment

Source: Internet
Author: User
Tags cairo gtk

Look at the effect first.

This is a very boring feature .... First of all, I do this function is not intended to achieve in the console display pictures ... (It seems so boring)

But because I want to do a game with C: http://q.cnblogs.com/q/65778/

Of course, this is a difficult and lengthy process, I decided to refer to someone else's code and not completely handling, by the way, because he is a Java programmer, learning C seems a bit difficult.

I'm going to start by drawing a map.

Pull it off, let's go.

SOIL (simple OpenGL Image Library), as its name implies, is simply the OpenGL image gallery

In fact, loading the picture of the learning process, I also learned a lot of other things, but are related to graphics, such as Gtk,opengl ...

Have experience to share it with, first of all, configuring GTK in the Visual Studio environment.

First download the full version of GTK (Windows) from here.

Then unzip the file to the location you want, here to unzip the file into the D packing directory, for later convenience, we changed the folder name to GTK

Then run cmd as an administrator, type the following command setx gtkdir d:\gtk/m

Then open VS2012, create a new C + + project, preferably an empty project.

Then right-click your project, click Properties---Configuration Properties->vc++ Directory

Locate the Include directory and library directory on the right

Then click on the text box, you will come out down the arrow, click, and then click Edit

Then click on the new row, create a new row, and in the included directory, separate the

$(GTKDIR)\lib\gtk-2.0\include$(GTKDIR)\lib\glib-2.0\include$(GTKDIR)\include
Added, $ (gtkdir) is just added under CMD, of course, you can also omit the above steps, directly add absolute path, of course, it depends on your preferences.
In the library directory, add:
$(GTKDIR)\lib

Then, select a subsystem---system, linker---Windows (/subsystem:windows)
Note that this place, later if the following error.

Error 1 LNK2019: unresolved external symbol [email protected], the symbol is referenced in function ___tmaincrtstartup C:\Users\li\documents\visual Studio 2012\p Rojects\win32project3\win32project3\msvcrtd.lib (crtexew.obj) win32project3

Well, we need to get this back.

Then cmd, into the D:\gtk\bin directory,

We're going to use the commands inside to do something:

The first is the input command:

pkg-config --cflags gtk+-2.0 --msvc-syntax > compilerflags.txt

Then you can find this file under D:\GTK or D:\gtk\bin, which reads:

-mms-bitfields-id:/gtk/include/gtk-2.0-id:/gtk/lib/gtk-2.0/include-id:/gtk/include/atk-1.0-id:/gtk/include/ cairo-id:/gtk/include/gdk-pixbuf-2.0-id:/gtk/include/pango-1.0-id:/gtk/include/glib-2.0-id:/gtk/lib/glib-2.0/ Include-id:/gtk/include-id:/gtk/include/freetype2-id:/gtk/include/libpng14

We'll put it here for the time being.

Then we'll enter:

pkg-config --libs gtk+-2.0 --msvc-syntax > linkerflags.txt
It will produce a file named Linkerflags.txt in the directory.
The contents are as follows:

/libpath:d:/gtk/lib gtk-win32-2.0.lib gdk-win32-2.0.lib atk-1.0.lib gio-2.0.lib pangowin32-1.0.lib gdi32.lib Pangocairo-1.0.lib gdk_pixbuf-2.0.lib pango-1.0.lib cairo.lib gobject-2.0.lib gmodule-2.0.lib gthread-2.0.lib Glib-2.0.lib Intl.lib

Then we right-click on the item, then click on the properties, click on the command line under C + +, there is a column blank other options, the first paragraph of the green text (compilerflags.txt) copied into the inside,

Then jump to the command line on the linker, and copy the contents of the second paragraph of the green Text (linkerflags.txt).

So far, you can quote GTK in the Include!

So far, it's done, if you see the other plug-ins later, want to quote in, you can also use a similar method, most of the time will be successful OH ~ ~

SOIL

Here to introduce the introduction of the soil method, the repetition of the place will not say, there is a little different I still want to talk about,

If directly follow the above method, will introduce success, but debugging run the program will be error, how to solve it?

Under the soil directory->projects The following files are listed below.

It's all engineering files.

Here we choose VC8 to do, I VS2012 under the VC8 success, VC9 failed, do not understand why.

Go back to the original project, right-click on the solution to add the existing project, navigate to the VC8 place, and add it in.

Then the reference is OK.

Then you will be able to compile successfully (there are some other methods on the Internet, I tried useless, I only know the method of quoting engineering).

OK, then I'll put the code out:

#include "graphics.h" #include <stdio.h> #include <windows.h> #include <conio.h> #include < math.h> #include <GL\glut.h> #include <GL\GL.h> #include <GL\GLU.h> #include <SOIL.h>//        Lightweight graphical controls//loading pictures gluint mytexture;void display (void) {glclear (gl_color_buffer_bit);    Glloadidentity ();        Gltranslatef (100.0f, 100.0f, 0.0f);    Glbindtexture (gl_texture_2d, mytexture);    Glbegin (gl_quads);    GLTEXCOORD2F (0.0f, 0.0f);    GLVERTEX2F (0.0f, 0.0f);    GLTEXCOORD2F (1.0f, 0.0f);    GLVERTEX2F (256.0f, 0.0f);    GLTEXCOORD2F (1.0f, 1.0f);    GLVERTEX2F (256.0f, 256.0f);    GLTEXCOORD2F (0.0f, 1.0f);    GLVERTEX2F (0.0f, 256.0f);        Glend (); Glutswapbuffers ();}        void reshape (int width, int height) {glviewport (0, 0, width, height);    Glmatrixmode (gl_projection);    Glloadidentity ();    gluortho2d (0, width, 0, height); Glmatrixmode (Gl_modelview);} void idle (void) {Glutpostredisplay ();} int main (int argc, char** argv) {glutinit (&aRGC, argv); Glutinitdisplaymode (Glut_rgba |    glut_double);    Glutinitwindowsize (640, 480);    Glutcreatewindow ("SOIL test");    Glutdisplayfunc (display);    Glutreshapefunc (reshape);        Glutidlefunc (idle); Mytexture = Soil_load_ogl_texture ("1.jpg", 0, 1, soil_flag_power_of_two |    SOIL_FLAG_INVERT_Y);        if (!mytexture) {printf ("Soil failed to load texture\n");    Exit (0);        } glenable (gl_texture_2d);    Glutmainloop (); return exit_success;}

In fact, I am also a beginner, so the code interpretation, I also dare not to talk.

And a little bit of a hit to notice, is loading the picture path, I here is 1.jpg, this 1.jpg is a relative path,

If the path is wrong, the picture is not loaded, the path is set as follows:

Right-click the project, under the configuration properties of the debugging inside a working directory, working directory is the relative path OH.

I myself because is out of practiced hand stage, so deleted some unnecessary code, actually began to write some square and line lines of code, here did not put out, but will put.

How to use soil to display pictures in a VS2012 C + + Environment

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.