Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Getting Started With Openlayer

Credits go to Fladimir da Gorf and Kauhiz for helping out!
This thread is locked; no one can reply to it. rss feed Print
Getting Started With Openlayer
Fasine
Member #8,221
January 2007

Okay, I got OpenLayer working and am thus far impressed by the demo programs (I compiled them myself in the same project as this program, so know the linker is setup correctly now.) And decided to write a quick test program of my own using the new commands for practice and quickly ran into trouble. This is the code:

1#include <openlayer.hpp>
2#include <allegro.h>
3#include <cstdlib.h>
4 
5main()
6{
7 Setup::SetupProgram;
8 Setup::SetupScreen( 1024, 768, WINDOWED, 32 );
9 if(!IsProgramSetup)
10 {
11 allegro_message("Failed to setup.");
12 return(1);
13 }
14 if(!IsProgramSetup)
15 {
16 allegro_message("Failed to setup screen.");
17 return(1);
18 }
19 Bitmap myBmp( "Grass.bmp" );
20
21 myBmp.Blit( 200,100);
22
23 GfxRend::RefreshScreen();
24 readkey();
25}
26END_OF_MAIN()

And this is the error I got: [EDIT]: It didn't actually return an error, just said that it failed, this is from the Compile Log.

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c "graphics test" -o "graphics test.o" -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

g++.exe: graphics test: linker input file unused because linking not done

g++.exe "graphics test.o" -o "Game3.exe" -L"C:/Dev-Cpp/lib" -mwindows -lopenlayer -lglyph -lfreetype -lloadpng -lpng -lz -lagl -lalleg -luser32 -lgdi32 -lglu32 -lopengl32

g++.exe: graphics test.o: No such file or directory

make.exe: *** [Game3.exe] Error 1

Execution terminated

No doubt I've done something dumb again. Who can tell me what? =)

PS- Do I need to set up a double buffer in OpenLayer? It looks like the GfxRend::RefreshScreen() command takes care of that, but I could be wrong, and if so: how do I go about it. Again: thanks for your replies, everyone.

Kauhiz
Member #4,798
July 2004

Quote:

Do I need to set up a double buffer in OpenLayer? It looks like the GfxRend::RefreshScreen() command takes care of that

You don't need a double buffer. However, AFAIK you're not supposed to use GfxRend anymore. You should use Canvas instead.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

if(!IsProgramSetup)

There's no global variables anywhere. You should test if the setup functions return false, instead.

Quote:

You don't need a double buffer. However, AFAIK you're not supposed to use GfxRend anymore. You should use Canvas instead.

I some of the old demos still use GfxRend. I'm changing those atm. The line should read "Canvas::Refresh()".

EDIT: Now the demos don't use GfxRend anymore.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Fasine
Member #8,221
January 2007

Okay, I changed the command to Canvas::Refresh(), but it still doesn't compile, unfortunately. I get the below error, accompanied by the same info in the compile log.

-C:\Dev-Cpp\Makefile.win [Build Error] [Game3.exe] Error 1

Does this line indicate a problem of some sort?

-g++.exe: graphics test: linker input file unused because linking not done

The linker is filled out with all the fields that made the demo programs work, so I'm not sure why it's not linking. Any suggestions, and is there more info about the problem I should be providing? (And where can I find it?) Thanks, as always.

Dustin Dettmer
Member #3,935
October 2003
avatar

Hows OpenLayer working on windows going? Does it have a working makefile yet?

juvinious
Member #5,145
October 2004
avatar

DDustin: if you want makefiles for mingw/msys you can generate them using cmake.

__________________________________________
Paintown

Fasine
Member #8,221
January 2007

Okay, I've made every (bad word) dumb mistake imagineable... It turns out that the project was failing to compile since there was a space in my filename (duh...) then it complained of a circular dependency because I forgot to suffix it with .cpp, and now it's giving me real compiler errors, saying that none of the OpenLayer commands have been declared, so I'm posting the source again, in hopes that someone can help me find the dumb mistake.

1#include <openlayer.hpp>
2#include <allegro.h>
3#include <cstdlib.h>
4 
5void main()
6{
7 Setup::SetupProgram;
8 Setup::SetupScreen( 1024, 768, WINDOWED, 32 );
9 if(!SetupProgram)
10 {
11 allegro_message("Failed to setup.");
12 return(1);
13 }
14 if(!SetupScreen)
15 {
16 allegro_message("Failed to setup screen.");
17 return(1);
18 }
19 Bitmap myBmp( "Grass.bmp" );
20
21 myBmp.Blit( 200,100);
22
23 Canvas::Refresh();
24 readkey();
25}
26END_OF_MAIN()

and in the linker:

-lopenlayer -lglyph -lfreetype -lloadpng -lpng -lz -lagl -lalleg
-luser32 -lgdi32 -lglu32 -lopengl32

Roughly the same Compile Log as before, which pobably just said "failed to compile" in elaborate terms. Again, thanks for your time everyone! Perhaps we'll make a game of it: "Who can find Fasine's dumb mistake fastest?!" =P

Fladimir da Gorf
Member #1,565
October 2001
avatar

Fixed the code:

1#include <OpenLayer.hpp>
2 
3// (Notice caps in OpenLayer.hpp)
4 
5using namespace ol; // You need this
6 
7int main() // If you want to "return 1;" you'll need to have int main()
8{
9 if(!Setup::SetupProgram()) // Note the braces and checking the return value
10 {
11 allegro_message("Failed to setup.");
12 return 1; // Return is not a function, so no braces there
13 }
14 if(!Setup::SetupScreen( 1024, 768, WINDOWED, 32 )) // Check the return value
15 {
16 allegro_message("Failed to setup screen.");
17 return 1;
18 }
19 Bitmap myBmp( "Grass.bmp" );
20
21 // Check if the bitmap exists
22 if(!myBmp) {
23 allegro_message("Can't find Grass.bmp");
24 return 1;
25 }
26
27 myBmp.Blit(200,100);
28
29 Canvas::Refresh();
30 readkey();
31 return 0; // main should always return 0 in success
32}
33END_OF_MAIN()

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Fasine
Member #8,221
January 2007

Thanks, Fladmir, that worked beautifully. One more dumb question though: how do I set up the myTextRenderer.Print function? It keeps saying it's undeclared, and I can't seem to use the textprintf_ex(screen,...) function, since I can't pass SCREEN_BACKBUF to it, and screen apparently has no effect when passed. Thanks a ton for all your help, Fladmir and everyone else, too. I think I'm jumping in at the deep end as far as re-learning C, but I don't give up easily, and these forums are a great help.

PS- "String" keeps coming up as undefined, what header file do I need to declare for it? <string.h> didn't work. Or is it within the myTextRenderer function?

Kauhiz
Member #4,798
July 2004

Quote:

how do I set up the myTextRenderer.Print function?

That's in the manual.

Quote:

I can't seem to use the textprintf_ex(screen,...) function, since I can't pass SCREEN_BACKBUF to it, and screen apparently has no effect when passed.

That's because SCREEN_BACKBUF isn't an allegro bitmap. And textprintf_ex works for screen, but since screen doesn't get drawn, you won't see it.

Quote:

"String" keeps coming up as undefined, what header file do I need to declare for it?

It's actually std::string. Put using namespace std at the top of you code (like you have for ol).

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Fasine
Member #8,221
January 2007

Aha, it works now. It appears that I have to load a font to use the text functions. In case anyone else has (or will have) this problem, the working code is:

1#include "OpenLayer.hpp"
2#include <allegro.h>
3#include <cstdlib.h>
4#include <string>
5 
6using namespace ol;
7using namespace std;
8 
9 
10int main()
11{
12 if(!Setup::SetupProgram())
13 {
14 allegro_message("Failed to setup.");
15 return 1;
16 }
17 if(!Setup::SetupScreen(1024, 768, WINDOWED, 32))
18 {
19 allegro_message("Failed to setup screen.");
20 return(1);
21 }
22
23 //Loads my bitmap to myBmp.
24 Bitmap myBmp( "Grass.bmp" );
25
26 if(!myBmp)
27 {
28 allegro_message("Can't find Grass.bmp");
29 return 1;
30 }
31
32 //I copied the Fonts folder to the project file's location.
33 //This loads the font.
34 TextRenderer neuropol("Fonts/neuropol.ttf", 10, 15, Rgba::BLACK);
35
36 int count=0;
37 while(!keypressed())
38 {
39 //blit myBmp to SCREEN_BACKBUF at coordinates 0,0
40 myBmp.Blit(0,0);
41
42 //converts the integer called count to a character string called
43 //message.
44 string message = ToString( count );
45 
46 //Prints the string stored in message to the screen at coordinates
47 //300,100.
48 neuropol.Print( message, 300, 110 );
49 
50 //display SCREEN_BACKBUF on the screen
51 Canvas::Refresh();
52 rest(1);
53 count++;
54 }
55 return 0;
56}
57END_OF_MAIN()

The program counts and displays the integer on the screen once per iteration, which is an overly simple way I chose to get a ballpark look at the possible framerate. Thanks for all the help, guys! (btw, it ran at around 600 fps, even with a rest(1) at the end. Huzzah for 600 frames in 400ms!!)

Fladimir da Gorf
Member #1,565
October 2001
avatar

You could always use OL's FpsCounter ;)

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Kauhiz
Member #4,798
July 2004

Uhm, I'm not sure if I understood what you're trying to do, but if you want the fps, you should look in the OpenLayer manual under FpsCounter. That's a simple way to get the fps and it gives you some other cool stuff as well ;) (namely delta timing).

Edit: beaten by Flad.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Dustin Dettmer
Member #3,935
October 2003
avatar

Flad said:

// Return is not a function, so no braces there

But I think return(1) looks cooler.. I would totally do it if I didn't hate typing extra characters...

Go to: