11 Replies - 605 Views - Last Post: 26 August 2012 - 04:06 PM Rate Topic: -----

#1 jtreiner  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 20-February 12

fstream wont work properly in my program.

Posted 26 August 2012 - 02:31 PM

Hello everyone,
I am making a Minecraft like game (please don't criticize me of making a "Minecraft ripoff") and i can't seem to get fstream to work. I am working on a Mac with Xcode and Openg/Glut. I receive no errors but when i load the data from the file i get 0's. I am trying to save data to a file then get the data using "<<" into a float array. If anyone could help me that would be great. :bigsmile:
Heres the code:



#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//file dec
ifstream Platformx_in;
ifstream Platformy_in;

ofstream Platformx_out;
ofstream Platformy_out;


//mouse vars
float mousex;
float mousey;
bool mouse_click = false;
bool mouse_click_right = false;

//camera vars
float camerax = 0.0f;
float cameray = 0.0f;
float camera_follow = 10.0f;

//inventory vars
int inventory_hide_counter = 0;
int inventory_show_time = 70;
bool inventory_show = false;

int inventory_item_select = 1;

//Player vars

enum direction{upd, leftd, rightd, downd, noned};
direction player_direction = noned;
direction player_directionx = noned;

bool secondary_click_a = false;
bool secondary_click_d = false;

float Playerx = 0.0f;
float Playery = 12.0f;

float Playerspeed = 0.2f;
float player_fallspeed = 0.50f;
float player_jumpspeed = 0.40f;

float Player_width = 3.0f;                                       //   From
float Player_hight = 6.0f;                                       //  Center

bool Player_is_colliding_platform = false;
bool Player_is_colliding_platform_top = false;
bool Player_is_colliding_platform_top_temp = false;
bool Player_is_colliding_platform_bottom = false;
bool Player_is_colliding_platform_left = false;
bool Player_is_colliding_platform_right = false;
bool Player_is_colliding_platform_down = false;
bool Player_is_colliding_platform_temp = false;
bool player_collision_switch = false;

bool player_is_jumping = false;
int player_jump_counter;
int player_jump_hight = 17;
bool player_left_jump = false;
bool player_right_jump = false;
bool player_jump_uneven_surface_left = false;
bool player_jump_uneven_surface_right = false;

//Platform vars
int num_platform = 1; //0 counts
const int num_platform_array = 10000 + 5;

bool platform_array = false;

float Platform_width[num_platform_array];
float Platform_hight[num_platform_array];

float Platformx[num_platform_array];
float Platformy[num_platform_array];

bool platform_shade[num_platform_array];

enum pixel {Stone, Dirt, Iron, None};
pixel type_of_pixel[num_platform_array];
pixel platform_inv_selected;
pixel inv_slot1 = Stone;
pixel inv_slot2 = Dirt;
pixel inv_slot3 = Iron;
pixel inv_slot4;
pixel inv_slot5;
pixel inv_slot6;
pixel inv_slot7;
pixel inv_slot8;
pixel inv_slot9;


bool platform_clicked[num_platform_array];

bool platform_is_falling[num_platform_array];
bool platform_will_fall;


//Iron vars
int Iron_hight = 6;


//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
	switch (key) {
        case 119: //W
            player_is_jumping = true;
            return;
        case 100: //D
            player_directionx = rightd;
            return;
        case 97: //A
            player_directionx = leftd;
            return;
        case 49:
            platform_inv_selected = inv_slot1;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 1;
            return;
        case 50:
            platform_inv_selected = inv_slot2;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 2;
            return;
        case 51:
            platform_inv_selected = inv_slot3;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 3;
            return;
        case 52:
            platform_inv_selected = inv_slot4;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 4;
            return;
        case 53:
            platform_inv_selected = inv_slot5;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 5;
            return;
        case 54:
            platform_inv_selected = inv_slot6;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 6;
            return;
        case 55:
            platform_inv_selected = inv_slot7;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 7;
            return;
        case 56:
            platform_inv_selected = inv_slot8;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 8;
            return;
        case 57:
            platform_inv_selected = inv_slot9;
            inventory_hide_counter = inventory_show_time;
            inventory_item_select = 9;
            return;
		case 27: //Escape key
			exit(0);
	}

}

void keyboardup(unsigned char key, int x, int y)
{
	switch (key) {
case 119:
    player_direction = noned;
    return;
case 100:
    player_directionx = noned;
    return;
case 115:
    player_direction = noned;
    return;
case 97:
    player_directionx = noned;
    return;
    }
		
}

void processMouseActiveMotion(int x, int y)
{
    if (mouse_click == true) {
            mousex = x;
            mousey = y;
            mouse_click = true;
            mousex /= 8.5333333333; //3.9353846153
            mousex -= 74.4;

            mousey /= 8.5365853658; //2.9733333333
            mousey -= 41.0;

            if (mousex > 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex > 0 && mousey > 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey > 0) {
                mousey = -mousey;
            }
            mousex += Playerx;
            mousey += Playery;
    }
    else if (mouse_click_right == true) {
        mousex = x;
        mousey = y;
        mousex /= 8.5333333333; //3.9353846153
        mousex -= 74.4;

        mousey /= 8.5365853658; //2.9733333333
        mousey -= 41.0;

        if (mousex > 0 && mousey < 0) {
            mousey = -mousey;
        }
        else if (mousex > 0 && mousey > 0) {
            mousey = -mousey;
        }
        else if (mousex < 0 && mousey < 0) {
            mousey = -mousey;
        }
        else if (mousex < 0 && mousey > 0) {
            mousey = -mousey;
        }
        mousex += Playerx;
        mousey += Playery;
        
        int mouse_x = mousex;
        int mouse_y = mousey;
        for (int n = 0; n < num_platform; n++) {
            if (mouse_x == Platformx[n]) {
                if (mouse_y == Platformy[n]) {
                    goto skip_place_platform;
                }
            }
        }
        num_platform++;
        type_of_pixel[num_platform] = platform_inv_selected;
        Platformx[num_platform] = mouse_x;
        Platformy[num_platform] = mouse_y;
        Platform_width[num_platform] = 0.5f;
        Platform_hight[num_platform] = 0.5f;
        
    }
        skip_place_platform:
    return;
}

void processMousePassiveMotion(int x, int y)
{
    mouse_click = false;
    mouse_click_right = false;
    mousex = x;
    mousey = y;
    mousex /= 8.5333333333; //3.9353846153
    mousex -= 74.4;

    mousey /= 8.5365853658; //2.9733333333
    mousey -= 41.0;
    if (mousex > 0 && mousey < 0) {
        mousey = -mousey;
    }
    else if (mousex > 0 && mousey > 0) {
        mousey = -mousey;
    }
    else if (mousex < 0 && mousey < 0) {
        mousey = -mousey;
    }
    else if (mousex < 0 && mousey > 0) {
        mousey = -mousey;
    }
    mousey += Playery;
    mousex += Playerx;

}

void processMouse(int button, int state, int x, int y)
{
    if(state == GLUT_DOWN)
    {
        if(button == GLUT_LEFT_BUTTON)
        {
            mouse_click_right = false;
            mousex = x;
            mousey = y;
            mouse_click = true;
            mousex /= 8.5333333333; //3.9353846153
            mousex -= 74.4;

            mousey /= 8.5365853658; //2.9733333333
            mousey -= 41.0;

            if (mousex > 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex > 0 && mousey > 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey > 0) {
                mousey = -mousey;
            }
            mousey += Playery;
            mousex += Playerx;
        }
        else if(button == GLUT_MIDDLE_BUTTON)
        {
            
        }
        else if(button == GLUT_RIGHT_BUTTON)
        {
            mouse_click = false;
            mouse_click_right = true;
            mousex = x;
            mousey = y;
            mousex /= 8.5333333333; //3.9353846153
            mousex -= 74.4;

            mousey /= 8.5365853658; //2.9733333333
            mousey -= 41.0;

            if (mousex > 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex > 0 && mousey > 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey < 0) {
                mousey = -mousey;
            }
            else if (mousex < 0 && mousey > 0) {
                mousey = -mousey;
            }
            mousey += Playery;
            mousex += Playerx;
            int mouse_x = mousex;
            int mouse_y = mousey;
            for (int n = 0; n < num_platform; n++) {
                if (mouse_x == Platformx[n]) {
                    if (mouse_y == Platformy[n]) {
                        goto skip_place_platform_2;
                    }
                }
            }
            num_platform++;
            type_of_pixel[num_platform] = platform_inv_selected;
            Platformx[num_platform] = mouse_x;
            Platformy[num_platform] = mouse_y;
            Platform_width[num_platform] = 0.5f;
            Platform_hight[num_platform] = 0.5f;
            
            
        
            return;
        }
    }
    else
    {
skip_place_platform_2:
        return;
    }
}



void Camera() {
    glTranslatef(mousex, mousey, 0.0f);
    glBegin(GL_QUADS);
    glColor3f(0.0f, 0.0f, 0.0f);
    glVertex2f(0.8f, 0.1f);
    glVertex2f(0.8f, -0.1f);
    glVertex2f(-0.8f, -0.1f);
    glVertex2f(-0.8f, 0.1f);
    
    glVertex2f(0.1f, 0.8f);
    glVertex2f(0.1f, -0.8f);
    glVertex2f(-0.1f, -0.8f);
    glVertex2f(-0.1f, 0.8f);
    glEnd();
    glTranslatef(-mousex, -mousey, 0.0f);
    
    if (inventory_show == true) {
    //inventory
    glTranslatef(Playerx, Playery + 20.0f, 0.0f);
    
    glTranslatef(36.0f, 0.0f, 0.0f);
        if (inventory_item_select == 9) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(-36.0f, 0.0f, 0.0f);
    glTranslatef(27.0f, 0.0f, 0.0f);
        if (inventory_item_select == 8) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(-27.0f, 0.0f, 0.0f);
    glTranslatef(18.0f, 0.0f, 0.0f);
        if (inventory_item_select == 7) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(-18.0f, 0.0f, 0.0f);
    glTranslatef(9.0f, 0.0f, 0.0f);
        if (inventory_item_select == 6) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(-9.0f, 0.0f, 0.0f);
    glTranslatef(0.0f, 0.0f, 0.0f);
        if (inventory_item_select == 5) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(-0.0f, 0.0f, 0.0f);
    glTranslatef(-9.0f, 0.0f, 0.0f);
        if (inventory_item_select == 4) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(9.0f, 0.0f, 0.0f);
    glTranslatef(-18.0f, 0.0f, 0.0f);
        
        glBegin(GL_QUADS);
        if (inv_slot3 == None) {
            glColor3f(0.0f, 0.0f, 1.0f);
        }
        else if (inv_slot3 == Dirt) {
            if (rand() % 9 > 5) {
                glColor3f(0.5f, 0.2f, 0.2f);
            }
            else {
                glColor3f(0.5f, 0.3f, 0.3f);
            }
        }
        else if (inv_slot3 == Stone) {
            if (rand() % 9 > 3) {
                glColor3f(0.6f, 0.6f, 0.6f);
            }
            else {
                glColor3f(0.65f, 0.65f, 0.65f);
            }
        }
        else if (inv_slot3 == Iron) {
            glColor3f(0.8f, 0.8f, 0.8f);
        }
        
        glVertex2f(2.0f, 2.0f);
        glVertex2f(2.0f, -2.0f);
        glVertex2f(-2.0f, -2.0f);
        glVertex2f(-2.0f, 2.0f);
        
        glEnd();
        
        if (inventory_item_select == 3) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(18.0f, 0.0f, 0.0f);
    glTranslatef(-27.0f, 0.0f, 0.0f);
        
        glBegin(GL_QUADS);
        if (inv_slot2 == None) {
            glColor3f(0.0f, 0.0f, 1.0f);
        }
        else if (inv_slot2 == Dirt) {
            if (rand() % 9 > 5) {
                glColor3f(0.5f, 0.2f, 0.2f);
            }
            else {
                glColor3f(0.5f, 0.3f, 0.3f);
            }
        }
        else if (inv_slot2 == Stone) {
            if (rand() % 9 > 3) {
                glColor3f(0.6f, 0.6f, 0.6f);
            }
            else {
                glColor3f(0.65f, 0.65f, 0.65f);
            }
        }
        else if (inv_slot2 == Iron) {
            glColor3f(0.8f, 0.8f, 0.8f);
        }
        
        glVertex2f(2.0f, 2.0f);
        glVertex2f(2.0f, -2.0f);
        glVertex2f(-2.0f, -2.0f);
        glVertex2f(-2.0f, 2.0f);
        
        glEnd();
        
        if (inventory_item_select == 2) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(27.0f, 0.0f, 0.0f);
        
    glTranslatef(-36.0f, 0.0f, 0.0f);
        
        glBegin(GL_QUADS);
        if (inv_slot1 == None) {
            glColor3f(0.0f, 0.0f, 1.0f);
        }
        else if (inv_slot1 == Dirt) {
            if (rand() % 9 > 5) {
                glColor3f(0.5f, 0.2f, 0.2f);
            }
            else {
                glColor3f(0.5f, 0.3f, 0.3f);
            }
        }
        else if (inv_slot1 == Stone) {
            if (rand() % 9 > 3) {
                glColor3f(0.6f, 0.6f, 0.6f);
            }
            else {
                glColor3f(0.65f, 0.65f, 0.65f);
            }
        }
        else if (inv_slot1 == Iron) {
            glColor3f(0.8f, 0.8f, 0.8f);
        }
        
        glVertex2f(2.0f, 2.0f);
        glVertex2f(2.0f, -2.0f);
        glVertex2f(-2.0f, -2.0f);
        glVertex2f(-2.0f, 2.0f);
        
        glEnd();

        if (inventory_item_select == 1) {
            glColor3f(1.0f, 1.0f, 1.0f);
        }
        else {
            glColor3f(0.55f, 0.55f, 0.55f);
        }
 
    glBegin(GL_QUADS);
    glVertex2f(4.0f, 4.0f);
    glVertex2f(4.0f, -4.0f);
    glVertex2f(-4.0f, -4.0f);
    glVertex2f(-4.0f, 4.0f);
    glEnd();
    glTranslatef(36.0f, 0.0f, 0.0f);
    
    glBegin(GL_QUADS);
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex2f(41.0f, 5.0f);
    glVertex2f(41.0f, -5.0f);
    glVertex2f(-41.0f, -5.0f);
    glVertex2f(-41.0f, 5.0f);
    glEnd();

    glTranslatef(-Playerx, -Playery - 20.0f, 0.0f);
    
    }
}
void terrain(int y, int x) {
    int Iron_h = y - Iron_hight;
    int Y = y;
    y += num_platform;
    for (int n = num_platform; n <= y; n++) {
        Y--;
        num_platform++;
        Platform_width[n] = 0.5f;
        Platform_hight[n] = 0.5f;
        Platformx[n] = -x;
        Platformy[n] = Y - 41;
        if (n == y) {
            if (rand() % 9 > 4) {
                type_of_pixel[num_platform + 3] = Dirt;
            }
            if (rand() % 9 > 3) {
                type_of_pixel[num_platform + 2] = Dirt;
            }
            if (rand() % 9 > 2) {
                type_of_pixel[num_platform + 1] = Dirt;
            }
            if (rand() % 9 > 1) {
                type_of_pixel[num_platform] = Dirt;
            }
        }
                if (rand() % 1000 > 998 && Y < Iron_h) {
                type_of_pixel[n] = Iron;
                }

        if (type_of_pixel[n] == None) {
            platform_shade[n] = false;
        }
        else if (type_of_pixel[n] == Dirt) {
            if (rand() % 9 > 5) {
                platform_shade[n] = false;
            }
            else {
                platform_shade[n] = true;
            }
        }
        else if (type_of_pixel[n] == Stone) {
            if (rand() % 9 > 3) {
                platform_shade[n] = false;
            }
            else {
                platform_shade[n] = true;
            }
        }
        else if (type_of_pixel[n] == Iron) {
            platform_shade[n] = false;
        }
        
        //store data in file
        
        
        Platformx_out.open("platformx.dat", ios::out | ios::binary);
        if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
        Platformx_out << Platformx[n] << endl;
        }
        else {
            cout << "Error";
        }
        Platformx_out.close();
        
        
        Platformy_out.open("platformy.dat", ios::out | ios::binary);
        if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
        Platformy_out << Platformy[n] << endl;
        }
        else {
            cout << "Error";
        }
        Platformy_out.close();

    }

}



void Player(float Platform_w, float Platform_h, float Platform_x, float Platform_y, pixel type, int n) {
    //check collisions
    //Platform

    
    if (Playery - Platform_y <= Platform_h + Player_hight ^ Platform_y - Playery >= Platform_h + Player_hight) {
        if (Playerx - Platform_x <= Platform_w + Player_width ^ Platform_x - Playerx >= Platform_w + Player_width) {
        Player_is_colliding_platform = true;
        Player_is_colliding_platform_temp = true;
        }
    }
    
    if (Player_is_colliding_platform_temp == true && Playery - Player_hight > Platform_y) {
        Player_is_colliding_platform_top = true;
        Player_is_colliding_platform_top_temp = true;
    }
    if (Player_is_colliding_platform_temp == true && Playery + Player_hight < Platform_y) {
        Player_is_colliding_platform_bottom = true;
        player_jump_counter = 0;
    }
    if (Player_is_colliding_platform_temp == true && Playerx - Player_width > Platform_x && Player_is_colliding_platform_top_temp == false) {
        Playerx += 0.05f;
        player_directionx = noned;
        if (Player_is_colliding_platform_top == false && player_jump_counter == 0 && Playery - 5.0 > Platform_y) {
            player_jump_uneven_surface_left = true;
        }
        else {
            player_jump_uneven_surface_left = false;
        }
    }
    if (Player_is_colliding_platform_temp == true && Playerx + Player_width < Platform_x && Player_is_colliding_platform_top_temp == false) {
        Playerx -= 0.05f;
        player_directionx = noned;
        if (Player_is_colliding_platform_top == true && player_jump_counter == 0 && Playery - 5.0 > Platform_y) {
            player_jump_uneven_surface_right = true;
        }
        else {
            player_jump_uneven_surface_right = false;
        }
    }
    Player_is_colliding_platform_temp = false;
    Player_is_colliding_platform_top_temp = false;
    

    //mouse
    int mouse_x = mousex;
    int mouse_y = mousey;
    if (mouse_click == true) {
    if (mouse_x == Platform_x & mouse_y == Platform_y) {
        platform_clicked[n] = true;
    }
    else {
        platform_clicked[n] = false;
    }
    }

    
    //jump
    if (player_is_jumping == true & Player_is_colliding_platform == true) {
        player_jump_counter = player_jump_hight;
        player_is_jumping = false;
    }
    
    glTranslatef(Playerx, Playery, 0.0f);
	
	glBegin(GL_QUADS);
	glColor3f(1.0f, 0.0f, 0.0f);
    
    glVertex2f(3.0f, 6.0f);
    glVertex2f(3.0f, -6.0f);
    glVertex2f(-3.0f, -6.0f);
    glVertex2f(-3.0f, 6.0f);
	
	glEnd();
    
    glTranslated(-Playerx, -Playery, 0.0f);
}

void Platform(float w, float h, float x, float y, pixel type, int n) {
    glTranslated(x, y, 0.0f);
    platform_is_falling[n] = false;
    platform_will_fall = true;
    
    if (type == Dirt) {
        for (int a = 0; a < num_platform; a++) {
            if (Platformy[a] == (y - 1.0f) && Platformx[a] == x) {
                platform_will_fall = false;
            }
        }
    }
    else {
        platform_will_fall = false;
    }
    if (platform_will_fall == true) {
    platform_is_falling[n] = true;
    }
    
    
    glBegin(GL_QUADS);
    if (type == None) {
        glColor3f(0.0f, 0.0f, 1.0f);
    }
    else if (type == Dirt) {
        if (platform_shade[n] == false) {
        glColor3f(0.5f, 0.2f, 0.2f);
        }
        else {
        glColor3f(0.5f, 0.3f, 0.3f);
        }
        Platform_width[n] = 0.5f;
        Platform_hight[n] = 0.5f;
    }
    else if (type == Stone) {
        if (platform_shade[n] == false) {
        glColor3f(0.6f, 0.6f, 0.6f);
        }
        else {
            glColor3f(0.65f, 0.65f, 0.65f);
        }
        Platform_width[n] = 0.5f;
        Platform_hight[n] = 0.5f;
    }
    else if (type == Iron) {
        glColor3f(0.8f, 0.8f, 0.8f);
        Platform_width[n] = 0.5f;
        Platform_hight[n] = 0.5f;
    }
    
    glVertex2f(w, h);
    glVertex2f(w, -h);
    glVertex2f(-w, -h);
    glVertex2f(-w, h);
    
    glEnd();
    
    glTranslated(-x, -y, 0.0f);
    
}



//Initializes 3D rendering
void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL); //Enable color
	glClearColor(0.6f, 0.7f, 1.0f, 1.0f); //Change the background to sky blue
}

//Called when the window is resized
void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = 30.0f;
float _cameraAngle = 0.0f;

//Draws the 3D scene
void drawScene() {
    if (platform_array == false) {
        
        srand ( time(NULL) );
        cout << rand() % 999999999 << rand() % 999999999;
        
        terrain(43, 75);
        terrain(42, 74);
        terrain(41, 73);
        terrain(40, 72);
        terrain(39, 71);
        terrain(38, 70);
        terrain(38, 69);
        terrain(37, 68);
        terrain(37, 67);
        terrain(37, 66);
        terrain(37, 65);
        terrain(37, 64);
        terrain(38, 63);
        terrain(39, 62);
        terrain(40, 61);
        terrain(42, 60);
        terrain(44, 59);
        terrain(45, 58);
        terrain(48, 57);
        terrain(48, 56);
        terrain(49, 55);
        terrain(51, 54);
        terrain(53, 53);
        terrain(54, 52);
        terrain(54, 51);
        terrain(55, 50);
        terrain(55, 49);
        terrain(55, 48);
        terrain(55, 47);
        terrain(55, 46);
        terrain(55, 45);
        terrain(55, 44);
        terrain(55, 43);
        terrain(54, 42);
        terrain(52, 41);
        terrain(52, 40);
        terrain(51, 39);
        terrain(50, 38);
        terrain(49, 37);
        terrain(47, 36);
        terrain(46, 35);
        terrain(45, 34);
        terrain(44, 33);
        terrain(43, 32);
        terrain(42, 31);
        terrain(41, 30);
        terrain(40, 29);
        terrain(39, 28);
        terrain(39, 27);
        terrain(39, 26);
        terrain(39, 25);
        terrain(38, 24);
        terrain(38, 23);
        terrain(38, 22);
        terrain(38, 21);
        terrain(38, 20);
        terrain(37, 19);
        terrain(36, 18);
        terrain(34, 17);
        terrain(33, 16);
        terrain(32, 15);
        terrain(32, 14);
        terrain(32, 13);
        terrain(32, 12);
        terrain(32, 11);
        terrain(32, 10);
        terrain(32, 9);
        terrain(32, 8);
        terrain(32, 7);
        terrain(32, 6);
        terrain(31, 5);
        terrain(31, 4);
        terrain(31, 3);
        terrain(31, 2);
        terrain(32, 1);
        terrain(30, 0);
        terrain(30, -1);
        terrain(31, -2);
        terrain(33, -3);
        terrain(35, -4);
        terrain(36, -5);
        terrain(42, -6);
        terrain(46, -7);
        terrain(47, -8);
        terrain(48, -9);
        terrain(48, -10);
        terrain(50, -11);
        terrain(50, -12);
        terrain(50, -13);
        terrain(50, -14);
        terrain(54, -15);
        terrain(56, -14);
        terrain(57, -15);
        terrain(58, -16);
        terrain(59, -17);
        terrain(61, -18);
        terrain(62, -19);
        terrain(63, -20);
        terrain(63, -21);
        terrain(64, -22);
        terrain(64, -23);
        terrain(65, -24);
        terrain(66, -25);
        terrain(67, -26);
        terrain(67, -27);
        terrain(67, -28);
        terrain(66, -29);
        terrain(65, -30);
        terrain(65, -31);
        terrain(64, -32);
        terrain(63, -33);
        terrain(63, -34);
        terrain(61, -35);
        terrain(60, -36);
        terrain(59, -37);
        terrain(57, -38);
        terrain(56, -39);
        terrain(54, -40);
        terrain(51, -41);
        terrain(51, -42);
        terrain(51, -43);
        terrain(51, -44);
        terrain(51, -45);
        terrain(51, -46);
        terrain(51, -47);
        terrain(51, -48);
        terrain(51, -49);
        terrain(51, -50);
        terrain(53, -51);
        terrain(54, -52);
        terrain(54, -53);
        terrain(55, -54);
        terrain(56, -55);
        terrain(58, -56);
        terrain(58, -57);
        terrain(60, -58);
        terrain(61, -59);
        terrain(61, -60);
        terrain(62, -61);
        terrain(62, -62);
        terrain(63, -63);
        terrain(63, -64);
        terrain(64, -65);
        terrain(65, -66);
        terrain(66, -67);
        terrain(66, -68);
        terrain(66, -69);
        terrain(66, -70);
        terrain(66, -71);
        terrain(66, -72);
        terrain(66, -73);
        terrain(66, -74);
        terrain(66, -75);
        platform_array = true;
    }
    
    srand ( time(NULL) );



    

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(-Playerx, -Playery, -60.0f);
    
    
    
    //load data from file
        
    Platformx_in.open("platformx.dat", ios::in | ios::binary);
    if (Platformx_in.is_open() && !Platformx_in.fail() && Platformx_in.good()) {
    Platformx_in.seekg(0, ios::beg);
        for (int a = 0; a < num_platform; a++) {
        Platformx[a] = 0.0f;
        Platformx_in >> Platformx[a];
        cout << Platformx[a] << ' ';
    }
    }
    else {
        cout << "Error";
    }
    Platformx_in.close();
    
    Platformy_in.open("platformy.dat" , ios::in | ios::binary);
    if (Platformy_in.is_open() && !Platformy_in.fail() && Platformy_in.good()) {
    Platformy_in.seekg(0, ios::beg);
        for (int a = 0; a < num_platform; a++) {
        Platformy[a] = 0.0f;
        Platformy_in >> Platformy[a];
    }
    }
else {
    cout << "Error";
    }
    Platformy_in.close();
        
    
    

    
    //reset variables
    Player_is_colliding_platform_down = false;
    Player_is_colliding_platform = false;
    Player_is_colliding_platform_top = false;
    Player_is_colliding_platform_left = false;
    Player_is_colliding_platform_right = false;
    Player_is_colliding_platform_bottom = false;
    player_jump_uneven_surface_left = false;
    player_jump_uneven_surface_right = false;
    
    

    Camera();
    for (int n = 0; n < num_platform; n++) {
    Player(Platform_width[n], Platform_hight[n], Platformx[n], Platformy[n], type_of_pixel[n], n);
    Platform(Platform_width[n], Platform_hight[n], Platformx[n], Platformy[n], type_of_pixel[n], n);
        if (platform_clicked[n] == true) {
            for (int i = n; i < num_platform; i++) {
            Platformx[i] = Platformx[i+1];
        
            Platformy[i] = Platformy[i+1];
            
            Platform_width[i] = Platform_width[i+1];
            
            Platform_hight[i] = Platform_hight[i+1];
                
            type_of_pixel[i] = type_of_pixel[i+1];
            }
            num_platform--;
            platform_clicked[n] = false;
        }
	};
    if (player_jump_uneven_surface_left == true ^ player_jump_uneven_surface_right == true) {
        player_jump_counter = 5;
    }

    // reset variables
    inventory_show = false;
        


	
	glutSwapBuffers();
}

void update(int value) {

    
    

    if (Player_is_colliding_platform_down == true & Player_is_colliding_platform == true) {
        if (player_directionx == leftd) {
            Playerx += 0.5f;
            player_directionx = noned;
        }
        else if (player_directionx == rightd) {
            Playerx -= 0.5f;
            player_directionx = noned;
        }
        player_direction = downd;
    }
    else if (Player_is_colliding_platform_top == false)
    {
        player_direction = downd;
    }
    if (player_jump_counter > 0) {
        player_direction = upd;
        player_jump_counter--;
    }
    

    

        
    
    if (player_direction == upd) {
        Playery += player_jumpspeed;
        mousey += player_jumpspeed;
    }
    else if (player_direction == downd)
    {
        Playery += -player_fallspeed;
        mousey += -player_fallspeed;
        player_direction = noned;
    }

    if (player_directionx == leftd)
    {
        Playerx += -Playerspeed;
        mousex += -Playerspeed;
    }
    else if (player_directionx == rightd)
    {
        Playerx += Playerspeed;
        mousex += Playerspeed;
    }

    //inventory
    if (inventory_hide_counter > 0) {
        inventory_hide_counter--;
        inventory_show = true;
    }
    
    
    //platform gravity
    for (int a = 0; a < num_platform; a++) {
        if (platform_is_falling[a] == true) {
            Platformy[a] -= 0.5f;
            platform_is_falling[a] = false;
        }
    }
    

    //block everload
    if (num_platform >= num_platform_array - 5) {
        exit(0);
    }
    




	glutPostRedisplay();
	glutTimerFunc(10, update, 0);
}



int main(int argc, char** argv) {

    
    
    
	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(1300, 900);                              //75 x 41
	
	//Create the window
	glutCreateWindow("PixelVerse 0.6 Alpha");
	initRendering();
	
	//Set handler functions
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
    glutKeyboardUpFunc(keyboardup);
    glutMotionFunc(processMouseActiveMotion);
    glutPassiveMotionFunc(processMousePassiveMotion);
    glutMouseFunc(processMouse);
	glutReshapeFunc(handleResize);
    
    

	
	glutTimerFunc(10, update, 0); //Add a timer

	
	glutMainLoop();
	return 0;
}



P.S.
Please don't comment on anything else besides things related to fstream and please don't copy code for your own use. Thanks.

sorry guys i meant i get the data using ">>" not "<<". :oops:

Is This A Good Question/Topic? 0
  • +

Replies To: fstream wont work properly in my program.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:09 PM

Quote

Please don't comment on anything else besides things related to fstream

Why are you using the formatted input operator>> to retrieve an item from a file you opened in binary mode?

You really need to rethink your program. Using so many global variables has made your program unworkable. Trying to find a problem is this program will be almost impossible. You need to learn to properly pass variables to and from your functions. Also a few data structures would probably greatly simplify your design.

Quote

please don't copy code for your own use. Thanks.

That won't be a problem your code is so bad I wouldn't think about using it except maybe for something to show people how not to program.


Jim

This post has been edited by jimblumberg: 26 August 2012 - 03:12 PM

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:09 PM

You could have at least told us which lines to look at within the 1000+ lines you posted.

Anyway, are you getting all zeros, or you get at least one or two good values and then you get zeros for the rest. If you could attach a copy of the input files, that would be helpful.
Was This Post Helpful? 1
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:23 PM

Your saving and loading routines need a rethink...

Your basically writing just one line of data then closing the file.

in a loop and you never specify ios::app when you open the file...

so it should remain whatever was the last piece of data written

then when you read the file...

you could use getline... it would be easier since you save it a line at a time.

ie getline(Platformx_in, Platformx[a]);

Snoopy.

Edit I have to agree with Skydiver as you really could have highlighted
the significant areas.

This post has been edited by snoopy11: 26 August 2012 - 03:25 PM

Was This Post Helpful? 2
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:28 PM

View Postjimblumberg, on 26 August 2012 - 10:09 PM, said:

Quote

please don't copy code for your own use. Thanks.

That won't be a problem your code is so bad I wouldn't think about using it except maybe for something to show people how not to program.


Jim


Again I have to agree with Jim here no one is going to steal your code
it just isn't interesting enough or well written enough...

So try to chill a bit.
Was This Post Helpful? 0
  • +
  • -

#6 jtreiner  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 20-February 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:40 PM

Sorry about my "bad code" this is only my second game i have ever made.
here are the most importent part of the code:


#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//file dec
ifstream Platformx_in;
ifstream Platformy_in;

ofstream Platformx_out;
ofstream Platformy_out;


#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//file dec
ifstream Platformx_in;
ifstream Platformy_in;

ofstream Platformx_out;
ofstream Platformy_out;

int num_platform = 1;
const int num_platform_array = 10000 + 5;

float Platformx[num_platform_array];
float Platformy[num_platform_array];



    for (int n = num_platform; n <= y; n++) {

        //store data in file
        
        Platformx_out.open("platformx.dat", ios::out | ios::binary);
        if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
        Platformx_out << Platformx[n] << endl;
        }
        else {
            cout << "Error";
        }
        Platformx_out.close();
        
        
        Platformy_out.open("platformy.dat", ios::out | ios::binary);
        if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
        Platformy_out << Platformy[n] << endl;
        }
        else {
            cout << "Error";
        }
        Platformy_out.close();

    }

    //load data from file
        
    Platformx_in.open("platformx.dat");
    if (Platformx_in.is_open() && !Platformx_in.fail() && Platformx_in.good()) {
    Platformx_in.seekg(0, ios::beg);
        for (int a = 0; a < num_platform; a++) {
        Platformx[a] = 0.0f;
        Platformx_in >> Platformx[a];
        cout << Platformx[a] << ' ';
    }
    }
    else {
        cout << "Error";
    }
    Platformx_in.close();
    
    Platformy_in.open("platformy.dat");
    if (Platformy_in.is_open() && !Platformy_in.fail() && Platformy_in.good()) {
    Platformy_in.seekg(0, ios::beg);
        for (int a = 0; a < num_platform; a++) {
        Platformy[a] = 0.0f;
        Platformy_in >> Platformy[a];
cout << Platformy[a] << endl;
    }
    }
else {
    cout << "Error";
    }
    Platformy_in.close();
    


if anyone can help that would be great.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:41 PM

Take the advice from snoopy11. His code has been stolen and reposted all over the Internet with the posters claiming to have written it. At least when I took some of Snoopy's code for my tutorials, I gave proper attribution.
Was This Post Helpful? 0
  • +
  • -

#8 jtreiner  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 20-February 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:49 PM

Im sorry i messed up the first block of code


#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//file dec
ifstream Platformx_in;
ifstream Platformy_in;

ofstream Platformx_out;
ofstream Platformy_out;

int num_platform = 1;
const int num_platform_array = 10000 + 5;

float Platformx[num_platform_array];
float Platformy[num_platform_array];



Snoopy11 I have tried your advice of using
 getline(Platformx_in, Platformx[a]);


but i get an error whenever i use getline. It says: no matching function for call to 'getline'.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:50 PM

The default open mode for an ofstream will open the file and erase the contents. You need to supply either the app, or ape open mode modifier to prevent the contents being erased. The way your files are now handled you will have at best one entry in the file.

Also why are you opening the file in binary mode? Normally you use the read/write member functions when you open the file in binary mode.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:50 PM

See snoopy11's post #4. Note the ios::app comment to fix your immediate issues, and also the entire post in general about restructuring your code.

This sort hints at that you've not even cracked open your files to make sure that data is being written out in the form you expecting.

This post has been edited by Skydiver: 26 August 2012 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#11 jtreiner  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 20-February 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 03:59 PM

I'm sorry guys, i am new to fstream and c++ itself can someone explain to me what i a need to do to fix my program. Thanks.
Was This Post Helpful? -1
  • +
  • -

#12 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: fstream wont work properly in my program.

Posted 26 August 2012 - 04:06 PM

View Postjtreiner, on 26 August 2012 - 03:59 PM, said:

can someone explain to me what i a need to do to fix my program. Thanks.

Yes. You need to drop this project and learn C++ properly. Go through several books, gain a strong understanding of how the language works. Work on small projects, simple projects, things that will help solidify the concepts in your mind. It is too soon for you to be working on any game, much less a 3D OpenGL game intending to clone Minecraft's gameplay.

Learn pointers, learn data structures, learn how to properly implement file I/O, learn how things are supposed to work before working on high-end, high-level projects.

If you want to work on 3D games, that's great. More power to you. But you have to practice, you have to learn, and you have to understand what you're doing before you do it. Make sure you get an understanding of 3D math, and get a solid understanding of how 3D APIs such as OpenGL and DirectX work before trying to use them. Otherwise, you'll end up with a situation such as this, where you're using global variables almost exclusively, without a single data structure in sight.

This post has been edited by Mina-no-Hime: 26 August 2012 - 04:07 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1