OpenGL with gl3w - c++

I'm trying to learn OpenGL. I'm using the SFML window module for the context and gl3w as my loading library. I use SFML pretty often so it wasnt a problem to set it up for OpenGL following this tutorial:http://www.sfml-dev.org/tutorials/2.0/window-opengl.php
I could run the example code without any problems. I linked everything that I need for OpenGL (opengl32.lib and glu32.lib + sfml*.lib).
Then I followed this answer to get gl3w :How to set up gl3w on Windows?.
But now if I try to run this code which is mostly the example code from SFML
#include <SFML/gl3w.h>
#include <SFML/Window.hpp>
static const GLfloat red[] = { 1.0f, 0.f, 0.f, 1.f };
int main()
{
// create the window
sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
gl3wInit(); //ignore return value for now
// load resources, initialize the OpenGL states, ...
// run the main loop
bool running = true;
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
//adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
glClearBufferfv(GL_COLOR, 0, red);
// end the current frame (internally swaps the front and back buffers)
window.display();
}
// release resources...
return 0;
}
I get following Linker errors.
1>OpenGL.obj : error LNK2019: unresolved external symbol _gl3wInit referenced in function _main
1>OpenGL.obj : error LNK2001: unresolved external symbol _gl3wViewport
1>OpenGL.obj : error LNK2001: unresolved external symbol _gl3wClearBufferfv
I double checked if I linked the libs correctly.
Im working on Windows 7 with Visual Studio 2013.
Anyone knows what I do wrong?

You forgot to compile gl3w.
Assuming that you already:
grabbed python script
ran it: python gl3w_gen.py
found gl3w.h glcorearb.h and gl3w.c files generated
There are two ways:
First way. Just include those files in your project, so it will be compiled along with your own sources. Note, that you need to preserve GL folder or manually fix includes in gl3w.c.
Second way. Create new project add all three files in it and compile as static library. Then link it to your application (or just compile it in command line or makefile, whatever).
Happy coding!

Related

Problem with SMFL Syntax in C++ Visual Studio importing png

-I am new to C++ and SFML and want to import a png file.
-It worked a view times, but afterwards I got the
message "Build failed, run last success?" most of the time. Sometimes its still working.
-there is no "real" error, so its hard to figure out, what the problem is
-I read earlier, that switching from Debug to Release-Mode could be a reason, but it didnt helped
its working when I dont use:
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
-> but, ofcourse, the sprite is missing then.
I would be happy to have a solution/reason for this or a topic I missed so far to read/ learn about.
Im happy about advices.
Thanks so far.
Alex
VISUAL STUDIO 2019
x64
SFML-2.5.1
WHOLE CODE:
#include"SFML/Graphics.hpp"
#include<iostream>
#include"main.h"
int main(int argc, char** argv[])
{
sf::RenderWindow window(sf::VideoMode(1200,800), "bimWindow");
sf::RectangleShape rs(sf::Vector2f(1000, 700));
rs.setFillColor(sf::Color::Green);
sf::Event event;
sf::Texture texture;
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100,100);
//sprite.scale(sf::Vector2f(3, 3));
rs.setPosition(80, 80);
// run the program as long as the window is open:
while (window.isOpen())
{
//let window open i guess.
while (window.pollEvent(event)); //stay true as long aas it didnt happen or so
{
// "close requested" event:close the window
if(event.type == sf::Event::Closed)
window.close();
}
//RENDER:
window.clear();
window.draw(rs);
window.draw(sprite);
window.display();
}
return 0;
}
Try making sure that the "assets/player.png" file is inside your solution directory (the folder with the .sln file.) Your code seems to run fine on my end, and that is the only error I can think of. If you can, it would help to have the error message(s) that you are receiving. If the png file is in the right place and you still get errors, I would recommend reinstalling SFML and following a tutorial online to ensure that you get everything set up properly.

Cannot load an image file as texture using C++ and SFML 2.1

I am trying to output an image onto the screen using SFML 2.1, C++, and MS Visual Studio Professional 2013. I am getting an unexpected error when trying to load a file into a texture. It outputs a whole bunch of random characters. I'm sure if its how I configured the SFML library with Visual Studio or a problem with the code. Can anyone solve this problem? Thanks.
Here is a screenshot of what it looks like when I run the program (http://i.stack.imgur.com/uMdLT.png):
This is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
For all configuration properties, they look like this:
Linker -> General (http://i.stack.imgur.com/NZg7P.png):
Linker -> Input (http://i.stack.imgur.com/1tPaB.png):
**Please note that if I did not configured the SFML library as I did, then I would be getting an error from my system saying msvcr110d.dll is missing.
Ok I managed to fix this, here is what you need to do:
Set your SUBSYSTEM to WINDOWS:
Add "sfml-main.lib" to your libraries:
Change your Runtime library to /MD. This is because you're not using the debug versions of the SFML 2.1 libraries (and probably can't in VS2013).
Make sure your "fighter jet.png" image is in the right place. By default Visual Studio uses the Project Directory as the working directory. So put the png in the same folder as your vcxproj file.
Are you sure that the picture is in the execution directory of your program ?
You should avoid spaces in file's name (fighter_jet.png).
If you're not sure about execution directory, try with absolute path (to be sure it's a path problem, and not a picture's problem).
I hope it helps.
I've tried this code on my system (Xcode - OSX), with one of my picture, and it works.
Have you tried with another picture ?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}

C++ Anti-aliased line for beginner to SDL

Original question:
I've been asked prior to a job interview to understand how an
anti-aliased line is drawn in a framebuffer, using C or C++. I haven't
used C, and it's been a few years for me since last using C++. I am a
complete beginner when it comes to graphics. My C++ experience has
mostly been in simple command-line programs and sorting methods. The
company does not care if I grab the code online, they want me to
understand it but still have a working executable.
I've used this tutorial to set up SDL libraries in MS VC++ 2012
Express, and this algorithm for the actual anti-aliasing. I have
a good understanding of the algorithm, though I'm currently having
trouble getting it to compile. I just want a line to be drawn, and
then I can go forward with setting the code up to the skeleton class
definitions I was given. This is what I have included aside from what
is on that page with the algorithm:
#include <cmath>
#include <math.h>
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "SDL.h"
const double HEIGHT = 240;
const double WIDTH = 320;
const double X0 = 25.6;
const double X1 = 64.7;
const double Y0 = 30;
const double Y1 = 42;
int round(double number)
{
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
}
void main()
{
Uint32 pixelColor = 00000000000000000000000000000000;
SDL_Surface* myScreen = SDL_CreateRGBSurface(SDL_ALPHA_OPAQUE,WIDTH,HEIGHT,32, 0x000000FF,
0x0000FF00, 0x00FF0000, 0xFF000000);
WULinesAlpha(X0, X1, Y0, Y1,pixelColor,myScreen);
return;
}
I'm getting the following errors:
Error 21 error LNK2019: unresolved external symbol _SDL_main
referenced in function _main Error 22 error LNK1120: 1 unresolved
externals
I've seen a few code examples saying the main function has to look
like this:
int main(int argc, char *argv[])
{
}
Again, graphics stuff is unfamiliar to me so I know my main function
is likely very wrong; I'm anticipating some shaking heads. Can someone
explain what is happening/what I need to do?
New:
I have now replaced my main function with the following code, based on NomNomNom069's YouTube video: "C++ SDL Tutorial 2 Creating a Screen and Handling Basic Input"
#include "SDL.h"
int main(int argc, char * args[])
{
bool running = true;
//initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
running = false;
}
//set up screen
SDL_Surface *screen;
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE);
if (screen == NULL)
{
running = false;
}
SDL_Event occur;
//main application loop
while (running)
{
SDL_PollEvent(&occur);
if (occur.type == SDL_QUIT)
{
running = false;
}
//drawing occurs here
SDL_FillRect(screen, NULL, 0);
SDL_Flip(screen);
}
//quit SDL
SDL_Quit();
return 0;
}
No errors, and I get a window to pop up. Awesome.
My question now is regarding how/where to call WuLinesAlpha. This function calls for 4 doubles, a Uint32 variable, and an SDL_Surface*. I have my doubles, I set the Uint32 to 0x000000FF, and I assume that the SDL_Surface I have set up as screen is the one passed in.
I've toyed around with where the WuLinesAlpha function call goes and I keep getting the black screen. I thought, as explained in the video, it would go in the loop but nothing has happened. Are there any more SDL commands I should be calling?
Fix your main declaration first. This does need to be int main(int argc, char *argv[]). Especially on Windows, since I believe SDL.h actually renames your main to some other name, and takes over main for the library itself.
Next, make sure you link against SDL properly. In my own SDL 1.2.x based project I have these lines in my Makefile:
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LFLAGS := $(shell sdl-config --libs)
I then later append those flags to my actual CFLAGS and LFLAGS. Note that if you use make and Makefiles, you want to use := there, otherwise make will invoke the $(shell ...) command every time it expands $(CFLAGS).
I can't help you set up Microsoft's GUI products. This tutorial, for a slightly older MSVC product (2010), looks pretty good, and may put you on the right track: http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet2010e/index.php
And finally, don't forget to call SDL_Init() at some point, preferably before you start creating surfaces.
Good luck!

A simple SDL application does not work with x64 configuration using Visual Studio 2012

I coded a simple application using SDL which displays a simple window on Windows 8 (64 bits). In a first time I compiled and executed my code with Win32 configuration (default configuration) and the program works perfectly. Now I want to have the same execution but this time with x64 configuration. So I configured Visual using 'configurations manager' in my project properties and change my SDL.lib and SDLmain.lib choosing x64 libraries in the linker. The project compilation is ok but the execution fails saying that the application has failed to start properly. Here's a screen of the message (the memory address is always the same at each execution) :
And my c++ code :
#include <iostream>
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <GL/glu.h>
#define WIDTH 500
#define HEIGHT 500
static float angle = 0.0f;
static void eventListener(SDL_Event *pEvent, bool *pContinue)
{
while (SDL_PollEvent(pEvent))
{
switch(pEvent->type)
{
case SDL_QUIT:
*pContinue = false;
break;
case SDL_KEYDOWN:
switch (pEvent->key.keysym.sym)
{
case SDLK_ESCAPE:
*pContinue = false;
break;
}
break;
}
}
}
#undef main
int main(void)
{
SDL_Event myEvent;
bool isAlive = true;
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Simple SDL window", NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
while (isAlive == true)
{
eventListener(&myEvent, &isAlive);
}
SDL_Quit();
return (0);
}
I don't understand this message which is not precise. However my x64 SDL libraries linked to my project seems to be correct because the compilation is ok. So I wonder what's happening here. Does anyone already have encountered the same problem ?
Just googled for your error message, and it says that this error code (0x0c000007b) means INVALID_IMAGE_FORMAT.
This means that either you are mixing 32 and 64 bit binaries or you have corrupted binaries. Try to place you binary and your dependencies at the same folder and run the application. If the error continues, than one of your libraries must be corrupted. Else, it was a problem with the Windows loading a library for a different platform of your compiled binary.

Mosync Build Error: Whats it mean

I am delving into Mosync & its going pretty good, but for some reason I am getting the following errors which I dont understand:
First error = "Error: Unresolved symbol '__ZN4MAUI6LayoutC1EiiiiPNS_6WidgetEii',"
Second Error = "Error: Unresolved symbol '__ZN4MAUI7ListBoxC1EiiiiPNS_6WidgetENS0 _18ListBoxOrientationENS0_20ListBoxAnimationTypeEb',"
I have commented below which line the error refers to. I have also cleaned my project & rebuilt, this worked the 1st time but now it wont work & its creating these types of errors for other creations of widgets such as labels. What does this error mean?
#include <MAUtil/Moblet.h>
#include <MAUI/Layout.h>
#include <MAUI/ListBox.h>
#include <MAUI/Label.h>
#include <MAUI/EditBox.h>
using namespace MAUtil;
using namespace MAUI;
/// Functions ///
float toFahrenheit( float fahrenVal );
float toCelsius(float celsiusVal );
class TemperatureMoblet : public Moblet
{
public:
TemperatureMoblet()
{
mainLayout = NULL;
mainListBox = NULL;
initScreen();
}
void keyPressEvent(int keyCode, int nativeCode)
{
// todo: handle key presses
}
void keyReleaseEvent(int keyCode, int nativeCode)
{
// todo: handle key releases
}
void initScreen()
{
mainLayout = new Layout( 0, 0, 100, 600, NULL, 1, 3 ); // ERROR HERE
mainListBox = new ListBox( 0, 0, 100, 200, mainLayout,
ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR,
true ); // 2nd error here
......
}
You have a linking error which means that your code compiled fine, but when trying to bring the compiled code together some parts are missing. In this case it seems to be the UI classes. Generally this is because maui.lib is missing under the additional libraries. It is however strange that it worked the first time, perhaps you accidentally changed to the debug configuration?
If go right click on project -> properties -> MoSync project -> Build Settings. Is maui.lib among the additional libraries? Check both for the release and debug configurations (it should be mauiD.lib in the debug configuration).
If it doesn't work perhaps you can paste the whole build log somewhere, including all the calls to pipe-tool.