C++ Compiler throwing error with GLUT - c++

The code was working fine but after I added the void projection(float), it's giving me unqaulified-id error inside the projection function. I'm using CodeBlocks IDE, MinGW. Please help me.
This is the error I'm getting:
||=== Build: Debug in L3 (compiler: GNU GCC Compiler) ===|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void projection(float)':|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|56|error: expected unqualified-id before '=' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|57|error: expected primary-expression before ',' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp| 57|error: expected primary-expression before ')' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void specialKeyboard(int, int, int)':|
||=== Build failed: 3 error(s), 15 warning(s) (0 minute(s), 0 second(s)) ===|
Moreover, if I comment everything inside projection, I get this error:
obj\Debug\src\Display.o||In function `glutInit_ATEXIT_HACK':|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `keyboardKey'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseX'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseY'|
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `toggleProjection'|
CallbackFunctions.h
#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H
#include "GL/freeglut.h"
unsigned char keyboardKey;
char *specialKeyboardKey ="";
char *modifier="";
char *mouseButtonPressed="";
char *mouseState="";
int mouseX, mouseY;
int toggleProjection=0;
char *view="";
/* Hardware Interrupts callback functions */
void display(); //called every single frame
void reshape(int width, int height); //called whenever the dimensions of the window are changed
void idle(); //when there is no interaction with the window
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
void specialKeyboard(int button, int x, int y); //captures special keys like: SHIFT ALT CTRL F1.....
void mouse(int button, int state, int x, int y); //captures the mouse clicks and their state
void mouseMotion(int x, int y); //captures the motion of the mouse pointer in the window
void projection(float asp);
#endif // CALLBACKFUNCTIONS_H
CallbackFunctions.cpp
#include "../include/CallbackFunctions.h"
#include "../include/Text.h"
extern int dim;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//Drawing stuff
glFlush();
glutSwapBuffers();
}
void idle()
{
display();
}
/* This callback function is called whenever the window is resized or reshaped */
void reshape(int width, int height)
{
//Width To Height Ratio
float w2h = (height>0)? (float)width/height : 1;
glViewport(0, 0, width, height);
projection(w2h);
}
void projection(float asp)
{
//Setting up the Viewing Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(toggleProjection==0)
{
glOrtho(-dim*asp, +dim*asp, -dim, +dim, -dim, +dim);
}
else
{
float fov = 55.0, near = asp/5, far = asp*5;
gluPerspective(fov, asp, near, far);
//To do
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* This callback function is called when there is no activity in the window */
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
}
keyboardKey = key;
glutPostRedisplay();
}
/* This callback function is called whenever a keyboard interrupt is encountered */
void specialKeyboard(int key, int state, int a)
{
int currentModifier = glutGetModifiers();
switch(currentModifier)
{
case GLUT_ACTIVE_SHIFT:
modifier = "SHIFT";
break;
case GLUT_ACTIVE_CTRL:
modifier = "CTRL";
break;
case GLUT_ACTIVE_ALT:
modifier = "ALT";
break;
}
switch(key)
{
case GLUT_KEY_F1:
if(toggleProjection=0)
{
toggleProjection = 1;
view = "perspective";
break;
}
else
{
toggleProjection = 0;
view = "orthographic";
break;
}
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
mouseButtonPressed = "Left";
else if(button == GLUT_RIGHT_BUTTON)
mouseButtonPressed = "Right";
if(state == GLUT_DOWN)
mouseState = "down";
else if(state == GLUT_UP)
mouseState = "up";
}
void mouseMotion(int x, int y)
{
mouseX = x;
mouseY = y;
}
/*void info()
{
displayTextAt(-130, -40, 0, "mouseButton: %s", mouseButtonPressed);
displayTextAt(-130, -50, 0, "mouseButtonState: %s", mouseState);
displayTextAt(-130, -60, 0, "mouseX: %d", mouseX);
displayTextAt(-130, -70, 0, "mouseY: %d", mouseY);
displayTextAt(-130, -80, 0, "keyboard key: %c", keyboardKey);
displayTextAt(-130, -90, 0, "modifier: %s", modifier);
displayTextAt(70, -90, 0, view);
}*/

This is really a global variable declaration in a header problem. I've included the pastebin code (with fixes) for posterity.
/*This is the CallbackFunctions.h ------------------ */
#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H
#include <GL/freeglut.h>
extern unsigned char keyboardKey;
/* Hardware Interrupts callback functions */
void display(); //called every single frame
void keyboard(unsigned char key, int x, int y); //whenever a keyboard key is pressed/interrupt occurs
#endif // CALLBACKFUNCTIONS_H
/*This is the CallbackFunctions.cpp ------------------ */
#include "CallbackFunctions.h"
unsigned char keyboardKey = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void keyboard(unsigned char key, int x, int y)
{
keyboardKey = key;
}
/* --------------- This is the Main.cpp ---------------- */
#include <GL/freeglut.h>
#include "CallbackFunctions.h"
#define WIDTH 700
#define HEIGHT 400
void setupGlutWindow(int *argc, char **argv, int width, int height);
void setupCallbackFunctions();
int main(int argc, char** argv)
{
setupGlutWindow(&argc, argv, WIDTH, HEIGHT);
setupCallbackFunctions();
glutMainLoop();
}
void setupGlutWindow(int *argc, char **argv, int width, int height)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(700, 500);
glutCreateWindow("Project L3");
glViewport(0, 0, width, height);
}
void setupCallbackFunctions()
{
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
}

Related

C++ OpenGL Draw Pixel

I'm learning how to use OpenGL by doing an online course and the practice problem asks to create a function to draw pixels:
void SetPixel(int x, int y, char r, char g, char b){}
I know I could do something like this to draw one:
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0);
glVertex2i(100,100);
glEnd();
However given the context of the problem it's not a valid solution.
The problem also asks to use the SetPixel function by calling it within a loop to randomly place pixels across the screen. I understand how to implement that, but it's the drawing the actual pixel I don't understand, especially within the context of the program given.
The program (using GLUT):
#include <stdlib.h> //- for exit()
#include <stdio.h> //- for sprintf()
#include <string.h> //- for memset()
#ifdef _WIN32
#include "libs/glut.h"
#include <windows.h>
#pragma comment(lib, "winmm.lib") //- not required but have it in anyway
#pragma comment(lib, "libs/glut32.lib")
#elif __APPLE__
#include <GLUT/glut.h>
#elif __unix__
#include <GL/glut.h>
#endif
#define FRAME_WIDE 1000
#define FRAME_HIGH 600
//====== Structs & typedefs =========
typedef unsigned char BYTE;
struct POINT2D {int x, y;};
//====== Global Variables ==========
BYTE pFrameL[FRAME_WIDE * FRAME_HIGH * 3];
BYTE pFrameR[FRAME_WIDE * FRAME_HIGH * 3];
int shade = 0;
POINT2D xypos = {0,0};
int stereo = 0;
int eyes = 10;
//===== Forward Declarations ========
void ClearScreen();
void DrawFrame();
void Interlace(BYTE* pL, BYTE* pR);
void BuildFrame(BYTE *pFrame, int view);
void OnIdle(void);
void OnDisplay(void);
void reshape(int w, int h);
////////////////////////////////////////////////////////
// Program Entry Poinr
////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
//-- setup GLUT --
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //GLUT_3_2_CORE_PROFILE |
glutInitWindowSize(FRAME_WIDE, FRAME_HIGH);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
//--- set openGL state --
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//-- register call back functions --
glutIdleFunc(OnIdle);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(reshape);
//-- run the program
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////
// Event Handers
////////////////////////////////////////////////////////
void OnIdle(void)
{
DrawFrame();
glutPostRedisplay();
}
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(FRAME_WIDE, FRAME_HIGH, GL_RGB,GL_UNSIGNED_BYTE, (GLubyte*)pFrameR);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////
void ClearScreen()
{
memset(pFrameL, 0, FRAME_WIDE * FRAME_HIGH * 3);
memset(pFrameR, 0, FRAME_WIDE * FRAME_HIGH * 3);
}
void Interlace(BYTE* pL, BYTE* pR)
{
int rowlen = 3 * FRAME_WIDE;
for (int y = 0; y < FRAME_HIGH; y+=2)
{
for (int x = 0; x < rowlen; x++) *pR++ = *pL++;
pL += rowlen;
pR += rowlen;
}
}
void DrawFrame()
{
ClearScreen();
if (!stereo) BuildFrame(pFrameR, 0);
else {
BuildFrame(pFrameL, -eyes);
BuildFrame(pFrameR, +eyes);
Interlace((BYTE*)pFrameL, (BYTE*)pFrameR);
}
}
////////////////////////////////////////////////////////
// Drawing Function
////////////////////////////////////////////////////////
void BuildFrame(BYTE *pFrame, int view)
{
// create a loop in here that uses SetPixel function to randomly place 10,000 pixels
}
void SetPixel(int x, int y, char r, char g, char b)
{
}
Can someone please help me work this out? How would I implement the SetPixel function elegantly?
Based on your configurations for the pixel format, the red component of the pixel at (x, y) is at index 3 * (y * width + x), and +1 / 2 for green / blue:
void SetPixel(int x, int y, char r, char g, char b)
{
if (x < 0 || x >= FRAME_WIDE || y < 0 || y >= FRAME_HIGH)
return; // out of bounds
int index = 3 * (y * FRAME_WIDE + x);
pFrame[index] = r;
pFrame[index+1] = g;
pFrame[index+2] = b;
}
Note that it is unclear whether SetPixel should write both buffers or there should be a flag indicating which one, so I just wrote pFrame as a placeholder.

I am trying to use structure in c++ but its not working

in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.

How to draw text letter by letter in glut?

I can't get my GLUT program to work properly.I want to draw text (a char array) letter by letter with some miliseconds interval. My program:
#include <string.h>
#include <windows.h>
#include <glut.h>
#include<iostream>
int hei = glutGet(GLUT_SCREEN_HEIGHT)/2;
int wid = glutGet(GLUT_SCREEN_WIDTH)/2;
void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};
void selectFont(int newfont)
{
font = fonts[newfont];
glutPostRedisplay();
}
void selectColor(float r, float g, float b)
{
glColor3f(r, g, b);
glutPostRedisplay();
}
char *msg;
void tick(void)
{
glutPostRedisplay();
}
void timer(int value) {
}
int element=0;
void output(int x, int y, char *string, bool center=true) {
int len;
len = strlen(string);
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, string[i]);
glPopMatrix();
glutSwapBuffers();
Sleep(500);
}
}
void outputmsg(int x, int y, bool center=true) {
int len;
len = strlen(msg);
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, msg[i]);
}
}
void updatemsg() {
msg="test msg letter by letter";
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
msg="|";
updatemsg();
if(msg!="|")
output(wid, hei, msg, true);
else
output(wid,hei,msg,true);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y) {
switch(key){
case 27:
exit(1);
break;
}
}
int main(int argc, char **argv)
{
int i, msg_submenu, color_submenu;
glutInit(&argc, argv);
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-mono")) {
font = GLUT_BITMAP_9_BY_15;
}
}
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(wid*2, hei*2);
glutCreateWindow("CHAT");
glutFullScreen();
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(tick);
selectFont(0);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
It does really weird stuff like erasing the previus one char then printing then printing the next one and the erased one. I tried everything what i could find about this topic on the forums. How can i fix it?
What you should do is also hold the length of the string you want to display and increment it every few milliseconds:
int len;
char *msg;
void incrementLength(int value){
if(msg[len]==0)return; //end of string stop the callback
len++;
glutTimerFunc(100, incrementLength, 0);
glutPostRedisplay();
}
void updatemsg() {
msg="test msg letter by letter";
len=0;
glutTimerFunc(100, incrementLength, 0);
}
And then in outputmsg (if you actually call it) you only loop until len:
void outputmsg(int x, int y, bool center=true) {
if(center){
glRasterPos2f((wid-len), y);
}
else {
glRasterPos2f(x, y);
}
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, msg[i]);
}
}
This code has a couple of issues:
for(int i=0;i<len;i++) {
glutBitmapCharacter(font, string[i]);
glPopMatrix();
glutSwapBuffers();
Sleep(500);
}
You are popping a matrix that you never pushed anywhere, and you are doing it len-many times.
You are swapping buffers without clearing the screen in-between... that's going to lead to undefined results (generally a garbled mess of characters from previous frames).
You really need to re-think how you've implemented this.
I would suggest you implement some elapsed time check in your display (...) function that determines which character to print, rather than doing this in a loop that calls Sleep (...).

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.

accessing element 0 in string array makes it crash

I am declaring the following array
string names[4] = { "grass" , "smallGrassBlock" , "dirt" , "sand" };
and then
extern string names [];`
However it crashes when I do this:
string ss=names[0];
All relevent code:
Main:
#include<stdlib.h>
#include<windows.h>
#include<GL/glut.h>
#include<GL/freeglut.h>
#include<iostream>
#include <vector>
#include "include/Block.h"
#include <string>
extern string names [];
void initTextures();
GLint GetTexture(string file);
void IdleFunction();
void removeBlock(int x,int y);
int xOffset,yOffset = 0;
using namespace std;
string to_string(int number);
void placeBlock(int x,int y);
void drawBitmapText(char *string, float x, float y, float z);
void reshape(int w, int h);
void render(void);
void keyboard(unsigned char c,int x,int y);
void mouse(int button,int state, int x, int y);
void arrows(int key, int x, int y );
std::vector <Block> blockList;
GLuint textures[1];
int screenXSize=800;
int screenYSize=800;
int selectedBlockType = 1;
int main(int argc, char ** argv){
blockList.push_back(Block(0,-111,-111));
string s = blockList.at(0).getName();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(screenXSize,screenYSize);
glutCreateWindow("TITLE");
glutDisplayFunc(render);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(IdleFunction);
glutSpecialFunc(arrows);
glutMouseFunc(mouse);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
initTextures();
glutMainLoop();
}
void initTextures(){
for(int i =0; i<=2; i++){
string file = "C:/Users/Samuel/Documents/Documents/projects/test/MapCreator/"+names[i]+".png";
textures[i]=GetTexture(file);
}
}
GLint GetTexture(std::string Filename)
{
GLuint tex_ID;
tex_ID = SOIL_load_OGL_texture(
Filename.c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO
| SOIL_FLAG_MIPMAPS
| SOIL_FLAG_MULTIPLY_ALPHA
| SOIL_FLAG_COMPRESS_TO_DXT
| SOIL_FLAG_DDS_LOAD_DIRECT
| SOIL_FLAG_INVERT_Y
);
if( tex_ID > 0 )
{
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, tex_ID );
return tex_ID;
}
else{
cout<<"Texture failed to load";
return 0;
}}
void arrows(int key, int x, int y ){
if(key==101){
selectedBlockType++;
if(selectedBlockType > blockList.at(0).getTotal()){
selectedBlockType=1;
}
}
if(key==103){
selectedBlockType--;
if(selectedBlockType < 0){
selectedBlockType=blockList.at(0).getTotal();
}
}
}
void IdleFunction()
{
glutPostRedisplay();
}
void mouse(int button,int state, int x, int y){
y+=15;
x-=15;
if(button==GLUT_RIGHT_BUTTON){
removeBlock(x,y);
}
if(button==GLUT_LEFT_BUTTON){
placeBlock(x,y);
}
}
void keyboard(unsigned char c,int x,int y){
//wasd
if(c==119){
yOffset+=30;
}
if(c==97){
xOffset-=30;
}
if(c==115){
yOffset-=30;
}
if(c==100){
xOffset+=30;
}
if (c==27){
exit(0);
}
}
void removeBlock(int x, int y){
y-= yOffset;
x+=xOffset;
y-=400;
y*=-1;
y+=400;
int xmod = x%30;
x-=xmod;
if(xmod>=15){
x+=30;
}
int ymod = y%30;
y-=ymod;
if(ymod>=15){
y+=30;
}
for(int i = 0; i<blockList.size();i++){
if(blockList.at(i).getX()==x){
if(blockList.at(i).getY()==y){
blockList.erase(blockList.begin()+i);
}
}
}
}
void placeBlock(int x, int y){
y-= yOffset;
x+=xOffset;
y-=400;
y*=-1;
y+=400;
//gets it on the grid lines
int xmod = x%30;
x-=xmod;
if(xmod>=15){
x+=30;
}
int ymod = y%30;
y-=ymod;
if(ymod>=15){
y+=30;
}
bool there=false;
for(int i = 0; i<blockList.size();i++){
if(blockList.at(i).getXMin()<=x&&blockList.at(i).getXMax()>=x&&blockList.at(i).getYMin()<=y&&blockList.at(i).getYMax()>=y){
there=true;
cout<<"cannot place there!"<<endl;
}
}
if(!there){
blockList.push_back(Block(selectedBlockType, x, y));
}
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0, (GLdouble) 800, 0, (GLdouble) 800);
glutReshapeWindow(screenXSize,screenYSize);
}
void drawBitmapText(char *string, float x, float y, float z)
{
char *c;
glRasterPos3f(x, y, z);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}
}
void render(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(int i = 0; i<blockList.size();i++){
blockList.at(i).render(xOffset,yOffset,textures[blockList.at(i).getType()]);
}
glColor3f(0,0,0);
//This creates the grid
//Should be broken every 30
for(float f = 0;f<800; f+=30){
glBegin(GL_LINE_STRIP);
glVertex2f(f,0);
glVertex2f(f,800);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2f(0,f);
glVertex2f(800,f);
glEnd();
}
glColor3f(0.35,0.35,0.35);
string s = to_string(selectedBlockType);
string ss=names[0];
cout<<"YAY"<<endl;
cout<<ss;
cout<<endl<<"Selected"<<selectedBlockType;
s=s+" "+names[selectedBlockType];
cout<<"YAY"<<endl;
char *c = (char*)s.c_str();
drawBitmapText(c,5,760,0);
glutSwapBuffers();
glFlush();
}
string to_string(int number){
string number_string = "";
char ones_char;
int ones = 0;
while(true){
ones = number % 10;
switch(ones){
case 0: ones_char = '0'; break;
case 1: ones_char = '1'; break;
case 2: ones_char = '2'; break;
case 3: ones_char = '3'; break;
case 4: ones_char = '4'; break;
case 5: ones_char = '5'; break;
case 6: ones_char = '6'; break;
case 7: ones_char = '7'; break;
case 8: ones_char = '8'; break;
case 9: ones_char = '9'; break;
}
number -= ones;
number_string = ones_char + number_string;
if(number == 0){
break;
}
number = number/10;
}
return number_string;
}
Block header:
#ifndef BLOCK_H
#define BLOCK_H
#include <string>
#include <iostream>
#include<GL/freeglut.h>
#include <soil.h>
using namespace std;
class Block
{
public:
int getXMax();
int getXMin();
int getYMin();
int getYMax();
Block(int,int,int);
int getX();
int getY();
static int getTotal();
int getType();
void render(int,int,GLuint);
string getName();
string getNType(int i);
virtual ~Block();
protected:
private:
int x,xsize,ysize,y,Type,size;
};
#endif // BLOCK_H
Block:
#include "../include/Block.h"
#include <string>
#include <iostream>
#include<GL/freeglut.h>
#include <soil.h>
using namespace std;
string names[4] = { "grass" , "smallGrassBlock" , "dirt" , "sand" };
Block::Block(int type, int X,int Y)
{
size=3;
Type=type;
x=X;
y=Y;
switch(Type){
case 0:
xsize=546;
ysize=217;
break;
case 1:
xsize = 200;
ysize=95;
break;
default:
xsize=30;
ysize=30;
break;
}
}
int Block:: getTotal(){
return 3;
}
int Block::getX(){
return x;
}
int Block::getY(){
return y;
}
int Block::getXMin(){
return x-(xsize/2);
}
int Block::getYMin(){
return y-(ysize/2);
}
int Block::getXMax(){
return x+(xsize/2);
}
int Block::getYMax(){
return y+(ysize/2);
}
int Block::getType(){
return Type;
}
string Block::getName(){
return names[Type];;
}
string Block::getNType(int i){
return names[i];
}
void Block:: render(int xOffset,int yOffset,GLuint texture){
if(texture==0){
}
x-=xOffset;
y-=yOffset;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_POLYGON);
glTexCoord2f(1,1); glVertex2f(x+(xsize/2), y+(ysize/2));
glTexCoord2f(1,0); glVertex2f(x+(xsize/2), y-(ysize/2));
glTexCoord2f(0,0); glVertex2f(x-(xsize/2), y-(ysize/2));
glTexCoord2f(0,1); glVertex2f(x-(xsize/2), y+(ysize/2));
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable( GL_BLEND );
glClearColor(1.0,1.0,1.0,0.0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glPopAttrib();
x+=xOffset;
y+=yOffset;
}
Block::~Block()
{
//dtor
}
Further information:
The program does not crash when getting any valid element other than 0, nor does it provide an error message.
It is not clear how you implemented your program. The following is the test case that I wrote. It ran fine without any problem using Microsoft C++ compiler (cl.exe).
Test.cpp
#include <iostream>
#include <string>
using namespace std;
extern string names[];
int main()
{
cout << names[0] << endl;
return 0;
}
Test2.cpp
#include <string>
using namespace std;
string names[4] = { "grass" , "smallGrassBlock" , "dirt" , "sand" };
Compile command:
cl.exe test.cpp test2.cpp
If your code is like this, then this works fine
main.cpp
extern string names[];
int main() {
string ss = names[3];
cout << ss << endl;
cin.get();
}
string.cpp
#include <string>
using namespace std;
string names[4] = { "grass" , "smallGrassBlock" , "dirt" , "sand" };
Note: Keep both the files in the same project.
OR compile both the .cpp files together
gcc main.cpp other.cpp