File not reading correctly?

Started by
4 comments, last by Hodgman 12 years, 10 months ago
EDIT: Problem changed, check most recent post.

I have the following code in my program:

static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };


I am getting the following errors:

Error 1 error C2059: syntax error : '{'
Error 2 error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Error 3 error C2059: syntax error : '{'
Error 4 error C2334: unexpected token(s) preceding '{'; skipping apparent function body
[/quote]

Basically I am trying to move from OpenGL 1.0 to OpenGL 3.1.
So I need these two items to specify the coordinates of the vertices and the vertex array.

What is the problem with this code?

I got it strait from a tutorial here:
Joe's Blog

Thanks in advance,
I have been looking at this for almost 15mins.

CoderWalker
If this post was helpful please +1 or like it !

Webstrand
Advertisement
There is no error in that code. It must be something wrong somewhere else.
Maby its not building because it's inside a struct?

Here is the first part of the program:

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <GL/glew.h>

#include "stdlib.h"
#include "iostream"
#include <fstream>
#include <sstream>
#include <deque>
#include "cstring"
using namespace std;

static struct
{
//Data
GLuint vertex_buffer, element_buffer;
GLuint textures[2];

static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };

//Shaders
GLuint vertex_shader, fragment_shader, program;
struct {
GLint fade_factor;
GLint textures[2];
} uniforms;

struct {
GLint position;
} attributes;

GLfloat fade_factor;
} resources;


I assumed it was the lines posted earlier, because if I comment them out the error goes away.
So there must be something conflicting?
If this post was helpful please +1 or like it !

Webstrand
I found out that the array was not suppose to be inside the struct, just the tutorial was misleading.

I think that the tutorial is using C opposed to C++.

It contains the following line:
file_contents(filename, &length);

I didn't have a library included with that command.
So I tried to replace it with a fstream command.

Here is what I have:

ifstream file (filename,ios::in);
if (file.good() ==false)
{
return 0;
}
file.seekg(0, ios::end);
int size = file.tellg();
source = new GLchar[size];
file.seekg(0,ios::beg);
file.read(source,size);
file.close();


The problem is that after reading the file it has garbage appended to the end. What am I doing wrong?

variable source contains:

#version 110

attribute vec2 position;

varying vec2 texcoord;

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(0.5) + vec2(0.5);
}?????????????????????????
If this post was helpful please +1 or like it !

Webstrand
How do you print the data in source? If you just print it as a string remember that a string ends with a null character '\0'. source will probably not contain a null character.

This will not work:
std::cout << source;
If you want to read text out of a file, and treat it as a c-string: source = new GLchar[size + 1];//extra byte for the null terminator
...
file.read(source,size);
...
source[size] = '\0';//add the null terminator

This topic is closed to new replies.

Advertisement