GLFW setting callback functions - c++

What data type to use when setting a callback functions in GLFW?
I try setting a function to type void but gives me a compile error. Changing it to void __stdcall gives me an RT error, but how do I use the typedef's of GLFW such as GLFW*fun? I think this is the right one to do. I really need an example source code.
btw, I define GLFW as GLFW_DLL
UPDATE
My code's look like this:
(I did it in three ways)
1
prototype. because these* are below main
this one gives me a compile error(invalid conversion)
void MouseClick(int, int);
void Keyboard(int, int);
//...
2
//appending `__stdcall` get compiled but gives me `RT error`
void __stdcall MouseClick(int, int);
void __stdcall Keyboard(int, int);
3
on my other project, I use GLFW_BUILD_DLL and compiles in MSVS with casting.
glfwSetKeyCallback((GLFWkeyfun)Keyboard);
but now, I can't do it in MinGW with the GLFW_DLL
void MouseClick(int x, int y) {
}
in main...
int main() {
//glfw intialization...
glfwSetKeyCallback(Keyboard);
//other setters
}
but, how do I do it this way?
GLFWmouseposfun MousePos {
}
//...

According to the documentation for GLFW v2.x the prototype for what you pass to glfwSetKeyCallback (etc.) is
void GLFWCALL functionname( int key, int action );
Here is a complete C program which will print out the key events:
#include <stdio.h>
#include <GL/glfw.h>
void GLFWCALL keyfun(int key, int action) {
printf("%d %d\n", key, action);
}
int main() {
glfwInit();
glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
glfwSetKeyCallback(keyfun);
while(1) {
glfwSwapBuffers();
}
}
If this code doesn't work you are likely linking to GLFW incorrectly.

Ahm, sorry my fault. I just didn't noticed that I have this Vec2f To_OGL_Coords(int x, int y) inside void MousePos(int x, int y) which I haven't included in my post.
As I said, it is Vec2f which I left out the 3rd(z) coordinate to NULL that causes the runtime error. Method number 2 should work here, same as Jacob Parker's answer. I just really don't know much the effects of those pointers if not used correctly.
(PS. I am working on 2D that's why I neglected the z-coordinate. My bad)

Related

Codeblocks Console Application Settings: Undefined reference to WinMain

Edit with respect to solution suggestion:
The solution for the author of the question was that he put the main function into a namespace. Then he added parameters to the main function, then he used extern "C" in order to get his main to be recognized as entry point.
Answers, which looked detailed suggested that the code author did not define a main/win main function (which was debated). I also don't use Visual C++, but a mingw version which works on my AMD computer (its a win 64 variant on a windows 10 home edition).
I have a problem, which seems frequently to find its way to StackOverflow and I could not locate any answer which helped me resolve my problem.
I used codeblocks and made a project as a console application.
This is the code I tried to compile and I keep on getting the "undefined WinMain" error.
I used the win64 MinGW compiler, which came with codeblocks
Compiler Flags were: (-std=C++11, -std=C++14, and -std=C++17. I used all of previous one at a time, before), -Wall, and -pedantic
This is the code and naturally, I was not able to resolve my problem. The attached images are "Build log" and "Build Messages" from the respective codeblock tabs of the "Logs & other window".
Please help.
#include <iostream>
using namespace std;
class Rectangle {
protected:
int width, height;
public:
Rectangle(int width = 0, int height = 0): width(width), height(height) {};
int get_width() const {return width;}
int get_height() const {return height;}
virtual void set_width(int width) {this->width = width;}
virtual void set_height(int height) {this->height = height;}
int area() const {return width * height;}
};
class Square : public Rectangle {
public:
Square(int size = 0) : Rectangle(size, size) {
set_width(size);
this->width = this->height = size;
}
void process(Rectangle &r){
r.set_height(10);
cout << "expected area was 30, got " << r.area() << std::endl;
}
int main() {
Rectangle r(3,4);
process(r);
std::cout << "Template" << std::endl;
return 0;
}
};
Possibly this is part of the solution:
I know that extra compiler flags can be created.
This is a warning, which showed, after I wrapped my main function with extern "C" as suggested by one of the answers in your solution suggestion
||warning: command line option '-std=c17' is valid for C/ObjC but not for C++|.
In my case I chose the option, where it said -std=c++17, still this warning came up.

extra qualification error in DevC++ IDE with TDM-GCC 4.9.2

I have a class function that is defined as follows:
class Output
{
private:
window* pWind;
public:
Output();
window* CreateWind(int, int, int, int);
void CreateDesignToolBar(); //Tool bar of the design mode
void CreateSimulationToolBar();//Tool bar of the simulation mode
window * getwindow()const;
void CreateStatusBar();
void CreateDrawArea();
Input* CreateInput(); //creates a pointer to the Input object
void ClearStatusBar(); //Clears the status bar
void ClearDrawArea(); //Clears the drawing area
void DrawAssign(Point Left, int width, int height, string Text, bool Selected = false);
void Output::Drawcondition(Point left, int width, int height, int t_width, int t_height, string Text, bool Selected = false);
When I compile the source in DevC++, I get:
33 7 C:\Users\user\source\repos\flowchart-designer-and-simulator\GUI\Output.h [Error] extra qualification 'Output::' on member 'Drawcondition' [-fpermissive]
What is this? How do I remove this error?
First of all, Dev-C++ is not a compiler, but an IDE (a fancy editor, simply said). It uses some kind of other compiler under the bonnet. Proabably gcc (from MINGW), I don't really remember, as Dev-C++ is quite dated tool.
Secondly, you have not given full code (EDIT: full code was added later), but based on the error I think you have declared a method inside of class and qualified it with that class name. This is incorrect, as qualification there is not needed.
I.e. you should do something like this:
class Test {
void test ();
};
Not something like this (which, I presume, you have tried):
class Test {
void Test::test ();
};

"Expression must have class type" error

I am working on my little touchscreen code for an embedded microcontroller. I had my code working using functions. But now I want to make it into a class. I get an error:
expression must have class type.
And I don't know what the problem is. I googled my problem and no clear solution was found. Here's my code:
main.cpp
#include "screen.h"
#include "mbed.h"
#include "stdio.h"
screen test();
int main(void)
{
while (1)
{
test.button(50,70,100,50,"button1"); // line where the compiler sees an error
}
}
screen.h
class screen{
public:
screen();
void init();
void button(int, int, int, int, string);
private:
int runningstatus; // 0 = stopped // 1 = failure // 2 = running
point p;
};
screen.cpp
#include "screen.h"
touch_tft TFT(p20,p18,p17,p16,p5, p6, p7, p8, p15,"TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs, reset
screen::screen(){
}
void screen::init()
{
TFT.claim(stdout); // send stdout to the TFT display
TFT.background(Black); // set background to black
TFT.foreground(White); // set chars to white
TFT.cls(); // clear the screen
TFT.set_orientation(3);
TFT.set_font((unsigned char*) Arial24x23);
TFT.locate(60, 100);
}
void screen::button(int x0, int y0, int length, int height, string caption)
{
TFT.rect(x0 ,y0 ,x0+length ,y0+height, LightGrey);
TFT.rect(x0-1 ,y0-1 ,x0+length+1 ,y0+height+1, LightGrey);
TFT.fillrect(x0-2,y0-2 ,x0+length-1 ,y0+height-1, Navy);
TFT.locate(x0+10, y0+10);
TFT.background(Navy);
TFT.printf("%s", caption);
}
Can someone tell me what the problem with this code is. Its driving me totaly nuts!
You need to change: screen test(); to screen test;. As it is right now, you're declaring a function named test that returns a screen, not defining an object named test of type screen.
This is quite well known as C++'s "most vexing parse" (a good term to search for if you want more information).

Compiler Hiccup in C++ and with .o Files

I've been trying to compile a multi-file project, but every time I try to use a void in player.cpp, I keep getting this error message, which appears that the player.o that is created during compilation has the same definition of void player_action(...). When I tried to use a void in the other files, the same problem occurs, with their corresponding .o file. However, if I use structs in any of the files, no problems occurs, and no "multiple definition" error occurs. In the lines below is the error message the compiler is giving me.
obj\Debug\player.o: In function `Z13player_actioniii':
D:/Projects/Blackmail Mailman/player.cpp:13: multiple definition of `player_action(int, int, int)'
obj\Debug\main.o:D:/Projects/Blackmail Mailman/player.cpp:13: first defined here
This is the code from player.cpp I used:
#include "include_files.cpp"
struct player_struct
{
int x;
int y;
int previous_x;
int previous_y;
int mode;
};
void player_action(int x, int y, int mode)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_RIGHT:;
};
};
};
};
What could be wrong and how can I fix it? I'm using Codeblocks with Mingw and Windows XP. I already checked the other files and there aren't any extra definitions of void player_action().
You never #include .cpp files, rather the .h files only.
If you need to access void player_action() from several parts of your program you should make a header file myapi.h which contains the following:
//myapi.h
#ifndef MYAPI_HEADER
#define MYAPI_HEADER
void player_action(int x, int y, int mode);
/* more function declarations */
#endif
The file which defines the function will be like this:
//player.cpp
#include "myapi.h"
void player_action(int x, int y, int mode)
{
/*...*/
}
and the file which uses it will be like this:
//main.cpp
#include "myapi.h"
void GameCycle()
{
/*...*/
player_action(0,0,0);
/*...*/
}
Never include objects definitions with #include, unless you know what you are doing. And even if you do know, you should think twice before doing so. Always use include guards (#ifndef ... #define .. #endif) - this will prevent multiple inclusion of your header.
These are the basic recommendations. I have seen a good explanation of such stuff in B. Stroustrup's 'The C++ programming language'

Unresolved External Symbol Occuring with Class Files

First point: I'm not a C++ expert, far from it. I glanced at it briefly almost a year ago and have not touched again until about 2 weeks ago when I decided to teach myself DirectX in C++. I have had my fair share of errors but have (for the most part) been able to solve them myself. I give up on this one though. As far as I can tell the problem resides in how I am using the .h and .cpp files of the mapCube class, so I will post those.
The errors themselves are few but infuriating: I get a LINK:2019 unresolved external symbol error regarding all the functions of mapCube except the constructors, it tells me they are referenced in the main program but claims they are not initialized. The second error I know of has to do with the checkColl function, in that function alone VC++ 2010 decides that x, y, and z are no longer a part of the mapCube class, it is rather perplexing.
The code:
mapCube.h
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
extern const float TILE_WIDTH, TILE_HEIGHT;
extern LPDIRECT3DDEVICE9 d3ddev;
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
#ifndef MAPCUBE_H
#define MAPCUBE_H
class mapCube{
struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; DWORD COLOR;}; //might be able to put these elsewhere
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE) //they are already in main, but mapCube needs them too
public:
LPD3DXMESH cubeMesh;
float x,y,z;
void setCoord(float, float, float);
D3DXVECTOR3 getCoord(){return D3DXVECTOR3(x,y,z);};
mapCube();
mapCube(float, float, float);
mapCube(float, float, float, D3DXCOLOR);
void draw(D3DXMATRIX);
void setColor(D3DXCOLOR);
int checkColl(D3DXVECTOR3, D3DXVECTOR3);
};
#endif
mapCube.cpp
#include "mapCube.h"
mapCube::mapCube()
{
x=0;
y=0;
z=0;
setColor(D3DCOLOR_XRGB(128,128,128));
}
mapCube::mapCube(float nx, float ny, float nz)
{
x=nx;
y=ny;
z=nz;
setColor(D3DCOLOR_XRGB(128,128,128));
}
mapCube::mapCube(float nx, float ny, float nz, D3DXCOLOR color)
{
x=nx;
y=ny;
z=nz;
setColor(color);
}
void mapCube::setCoord(float nx, float ny, float nz) //this function and the next one are both called
{ //when the cube is created because I'm using
x=nx; //an array of cubes instead of one-by-one
y=ny;
z=nz;
};
void mapCube::setColor(D3DXCOLOR color) //basically just colors each vertex 'color'
{
LPD3DXMESH tmpMesh=NULL;
D3DXCreateBox(d3ddev, TILE_WIDTH, TILE_HEIGHT, TILE_WIDTH, &tmpMesh, NULL);
tmpMesh->CloneMeshFVF( 0, CUSTOMFVF, d3ddev, &cubeMesh );
LPDIRECT3DVERTEXBUFFER9 tmpVertBuf=NULL;
if( SUCCEEDED(cubeMesh->GetVertexBuffer(&tmpVertBuf)))
{
int nNumVerts = cubeMesh->GetNumVertices();
CUSTOMVERTEX *pVertices = NULL;
tmpVertBuf->Lock( 0, 0, (void**)&pVertices, 0 );
{
int i=0;
while(i<nNumVerts)
{
pVertices[i].COLOR=color;
i++;
}
}
tmpVertBuf->Unlock();
tmpVertBuf->Release();
}
};
void mapCube::draw(D3DXMATRIX matWorld)
{
D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate,x,y,z); //translation to the cubes stored coordinates
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate * matWorld)); //...combined with the total world transform
cubeMesh->DrawSubset(0);
};
int checkColl(D3DXVECTOR3 vecTest, D3DXVECTOR3 vecThis) //2nd arg bc compiler decided to forget
{ //that this class has x,y,z vars
if(vecTest.x>=vecThis.x-(TILE_WIDTH/2.0f) || vecTest.x<=vecThis.x+(TILE_WIDTH/2.0f)) //rudimentary attempt at collision checking
{
return 1;
}
else if(vecTest.z>=vecThis.z-(TILE_HEIGHT/2.0f) || vecTest.z<=vecThis.z+(TILE_HEIGHT/2.0f))
{
return 2;
}
else
{
return 0;
}
}
Sorry if the formatting is a bit off, this is the first time I have used this interface. The compiler reports no syntax errors or otherwise, after I change the x/z references in the last function to a passed D3D vector anyway. Any help/criticism is welcome, whether it relates to d3d or c++. I didn't post the main source because I do not believe the problem is there, but if asked I will post it as well.
Following the solution of that problem, I now notice this warning:
1>Debug\mapCube.obj : warning LNK4042: object specified more than once; extras ignored
I removed the duplicate definitions of CUSTOMVERTEX and FVF and made them extern in the header, but am unable to resolve these problems.
Perhaps you forgot to add mapCube.cpp to your VC++ project. Add this line to the top of mapCube.cpp:
#error This file is indeed being compiled
If that line does not generate a compiler error, then mapCube.cpp is not being compiled.
Try Rebuild Solution. Sometimes things get out of sync, and you just have to recompile everything from scratch.
Just thought it is worth pointing to the bleeding obvious, but is mapCube.cpp included in your project?
The second issue is easily solved: the checkColl implementation (in mapCube.cpp) is missing its mapCube:: prefix. This causes the compiler to think it is a "free function", not a member function of mapCube.