5 Replies - 3619 Views - Last Post: 19 September 2012 - 10:15 PM Rate Topic: -----

#1 jtreiner   User is offline

  • D.I.C Head

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

I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 04:58 PM

Here is the main part of the code I am having trouble with:
        top_io.open("type_of_pixel.txt", ios::out | ios::in);
        if (top_io.is_open()) {
            top_io.seekg(0, ios::beg);
            top_io.seekp(0, ios::beg);
            int test = 0;
            for (long a = 0; !top_io.eof(); a++) {
                top_io >> test;
                if (a == Platform_del) {
                    top_io << 0;
                    top_io >> test;
                    cout << endl << test;
                    top_io.close();
                    mouse_was_clicked = false;
                    goto top_io_finished;
                }
            }
            
        
    }
        else if (top_io.fail()) {
            cout << " ERROR ";
        }


when the program prints data the the console in the code
  cout << endl << test; 
I should get the result 0 but I don't. If anyone needs the full code just ask and I will post it.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: I am not getting the results I want from my program using fstream

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,625
  • Joined: 05-May 12

Re: I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 05:40 PM

On what basis are you making the assertion that you should get a result of zero?

As you probably figured out there are two seek pointers, since you have lines 3 and 4. You have to realize, though that these two pointers are not synchronized.

On line 7, you did an extraction as so the read seek pointer has been move forward. Since you didn't show us what the value of Platform_del is, we don't know how many times the read seek pointer is advanced due to line 7 within the for loop. On line 9, you write out to the file and advance the write seek pointer. On line 10, you do another read.

Assuming that you aren't getting any read errors, at the very least, the read seek pointer is ahead of the write seek pointer. So even though you wrote out a zero on line 9, the read pointer is already past the location where the zero was written out.
Was This Post Helpful? 1
  • +
  • -

#3 jtreiner   User is offline

  • D.I.C Head

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

Re: I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 05:48 PM

Ok but I still don't think my code is writing to the file properly. I am making a game and i only made the program print part of the file to show you it wasn't working. so is there any problem with the writing of the file?
Was This Post Helpful? 0
  • +
  • -

#4 jtreiner   User is offline

  • D.I.C Head

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

Re: I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 09:16 PM

I will post my full code incase it i needed:


#include <iostream>
#include <stdlib.h>
#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;
ifstream top_in;
ifstream find;

ofstream Platformx_out;
ofstream Platformy_out;
ofstream top_out;

fstream top_io;

int in_counter = 0;


//mouse vars
float mousex;
float mousey;
bool mouse_click = false;
bool mouse_click_right = false;
bool mouse_was_clicked = 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;
        
        
        Platformx_out.open("platformx.txt", ios::app);
        if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
            Platformx_out << mouse_x << endl;
        }
        else {
            cout << "Error";
        }
        Platformx_out.close();
        
        
        Platformy_out.open("platformy.txt", ios::app);
        if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
            Platformy_out << mouse_y << endl;
        }
        else {
            cout << "Error";
        }
        Platformy_out.close();
        
        
        top_out.open("type_of_pixel.txt", ios::app);
        int c;
        if (top_out.is_open() && !top_out.fail() && top_out.good()) {
            
            
            if (platform_inv_selected == Stone) {
                c = 1;
            }
            else if (platform_inv_selected == Dirt) {
                c = 2;
            }
            else if (platform_inv_selected == Iron) {
                c = 3;
            }
            
            top_out << c << endl;
        }
        else {
            cout << "Error";
        }
        top_out.close();
        
        if (type_of_pixel[num_platform] == None) {
            platform_shade[num_platform] = false;
        }
        else if (type_of_pixel[num_platform] == Dirt) {
            if (rand() % 9 > 5) {
                platform_shade[num_platform] = false;
            }
            else {
                platform_shade[num_platform] = true;
            }
        }
        else if (type_of_pixel[num_platform] == Stone) {
            if (rand() % 9 > 3) {
                platform_shade[num_platform] = false;
            }
            else {
                platform_shade[num_platform] = true;
            }
        }
        else if (type_of_pixel[num_platform] == Iron) {
            platform_shade[num_platform] = false;
        }

        
    }
        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;
            
            
            Platformx_out.open("platformx.txt", ios::app);
            if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
                Platformx_out << mouse_x << endl;
            }
            else {
                cout << "Error";
            }
            Platformx_out.close();
            
            
            Platformy_out.open("platformy.txt", ios::app);
            if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
                Platformy_out << mouse_y << endl;
            }
            else {
                cout << "Error";
            }
            Platformy_out.close();
            
            
            top_out.open("type_of_pixel.txt", ios::app);
            int c;
            if (top_out.is_open() && !top_out.fail() && top_out.good()) {
                
                
                if (platform_inv_selected == Stone) {
                    c = 1;
                }
                else if (platform_inv_selected == Dirt) {
                    c = 2;
                }
                else if (platform_inv_selected == Iron) {
                    c = 3;
                }
                
                top_out << c << endl;
            }
            else {
                cout << "Error";
            }
            top_out.close();
            
            
            if (type_of_pixel[num_platform] == None) {
                platform_shade[num_platform] = false;
            }
            else if (type_of_pixel[num_platform] == Dirt) {
                if (rand() % 9 > 5) {
                    platform_shade[num_platform] = false;
                }
                else {
                    platform_shade[num_platform] = true;
                }
            }
            else if (type_of_pixel[num_platform] == Stone) {
                if (rand() % 9 > 3) {
                    platform_shade[num_platform] = false;
                }
                else {
                    platform_shade[num_platform] = true;
                }
            }
            else if (type_of_pixel[num_platform] == Iron) {
                platform_shade[num_platform] = false;
            }

            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 c;
    int Iron_h = y - Iron_hight;
    int Y = y;
    y += num_platform;
    Platformx_out.open("platformx.txt", ios::app);
    Platformy_out.open("platformy.txt", ios::app);
    top_out.open("type_of_pixel.txt", ios::app);
    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
        
        
        
        if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
        Platformx_out << Platformx[n] << endl;
        }
        else {
            cout << "Error";
        }
        
        
        
        
        if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
        Platformy_out << Platformy[n] << endl;
        }
        else {
            cout << "Error";
        }
        
        
        
        
        if (top_out.is_open() && !top_out.fail() && top_out.good()) {
            
            
            if (type_of_pixel[n] == Stone) {
                c = 1;
            }
            else if (type_of_pixel[n] == Dirt) {
                c = 2;
            }
            else if (type_of_pixel[n] == Iron) {
                c = 3;
            }
            
            top_out << c << endl;
        }
        else {
            cout << "Error";
        }

    }
    Platformx_out.close();
    Platformy_out.close();
    top_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;
        mouse_was_clicked = 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);
        Platform_width[n] = 0.5f;
        Platform_hight[n] = 0.5f;
    }
    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;
        
        Platformx_out.open("platformx.txt");
        if (Platformx_out.is_open() && !Platformx_out.fail() && Platformx_out.good()) {
        }
        else {
            cout << "Error";
        }
        Platformx_out.close();
        
        
        Platformy_out.open("platformy.txt");
        if (Platformy_out.is_open() && !Platformy_out.fail() && Platformy_out.good()) {
        }
        else {
            cout << "Error";
        }
        Platformy_out.close();
        
        
        top_out.open("type_of_pixel.txt");
        if (top_out.is_open() && !top_out.fail() && top_out.good()) {

        }
        else {
            cout << "Error";
        }
        top_out.close();
        
        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);
    
    
    
    long Platform_del = 0;
    long file_size = 0;
    in_counter++;
    //load data from file
    if (in_counter == 20) {
        in_counter = 0;
        int load_count[num_platform_array];
        num_platform = 1;
        
    Platformx_in.open("platformx.txt");
    if (Platformx_in.is_open() && !Platformx_in.fail() && Platformx_in.good()) {
    Platformx_in.seekg(0, ios::beg);
        int count = 0;
        float move;
        int la = Playerx + 52;
        int lb = Playerx - 52;
        for (long a = 0; !Platformx_in.eof(); a++) {
        Platformx_in >> move;
            if (move < la && move > lb) {
                num_platform++;
                count++;
                Platformx[count] = move;
                load_count[count] = a;
                
                
            }
    }
    }
    else {
        cout << "Error";
    }
    Platformx_in.close();
    
    Platformy_in.open("platformy.txt");
    if (Platformy_in.is_open() && !Platformy_in.fail() && Platformy_in.good()) {
        int count = 0;
        float move = 0;
    Platformy_in.seekg(0, ios::beg);
        for (long a = 0; !Platformy_in.eof(); a++) {
            Platformy_in >> move;
            if (a == load_count[count + 1]) {
                count++;
        Platformy[count] = 0.0f;
        Platformy[count] = move;
                file_size++;
                int mouse_x = mousex;
                int mouse_y = mousey;
                int Platx = Platformx[count];
                int Platy = Platformy[count];
                if (Platx == mouse_x && Platy == mouse_y && mouse_was_clicked == true) {
                    Platform_del = a;
                }
            }
    }
    }
else {
    cout << "Error";
    }
    Platformy_in.close();
        

        
        
        
    top_in.open("type_of_pixel.txt");
    if (top_in.is_open() && !top_in.fail() && top_in.good()) {
        int count = 0;
        top_in.seekg(0, ios::beg);
        int c;
        for (long a = 0; !top_in.eof(); a++) {
            top_in >> c;
            if (a == load_count[count + 1]) {
                count++;
            if (c == 0) {
                type_of_pixel[count] = None;
            }
            if (c == 1) {
                type_of_pixel[count] = Stone;
            }
            else if (c == 2) {
                type_of_pixel[count] = Dirt;
            }
            else if (c == 3) {
                type_of_pixel[count] = Iron;
            }
            }
            

            
        }
    }
    else {
        cout << "Error";
    }
    top_in.close();
    
        
    if (mouse_was_clicked == true) {
        
        
        top_io.open("type_of_pixel.txt", ios::out | ios::in);
        if (top_io.is_open()) {
            top_io.seekg(0, ios::beg);
            top_io.seekp(0, ios::beg);
            int test = 0;
            for (long a = 0; !top_io.eof(); a++) {
                top_io >> test;
                if (a == Platform_del) {
                    top_io << 0;
                    top_io >> test;
                    cout << endl << test;
                    top_io.close();
                    mouse_was_clicked = false;
                    goto top_io_finished;
                }
            }
            
        
    }
        else if (top_io.fail()) {
            cout << " ERROR ";
        }
    }
    }

top_io_finished:
    

    
    //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_out.close();
            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;
}


Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 09:22 PM

Are you wanting to use binary mode? If not, and you want to use text mode, the write will need delimiters to separate the numbers, and probably a fixed width.

Here the file is opened in binary mode. The read and write functions are used to read and write binary.

The get and put positions are synchronized (the same value), on my system anyway. If you run this they are printed.

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
  fstream top_io;

  // open in binary mode
  top_io.open("type_of_pixel.txt", ios::out | ios::in  | ios::binary); 

  if( !top_io.is_open() )
  {
    cout << "top_io : Open error " << endl;
  }

  if( top_io.is_open() )
  {
    cout << "top_io opened" << endl;

    top_io.seekg(0, ios::beg);
    top_io.seekp(0, ios::beg);

    // report get and put positions
    cout << " tellg=" << top_io.tellg();
    cout << " tellp=" << top_io.tellp() << endl;

    int test = 0x4141;
    char *buf = (char *) &test;

    for (long a = 0; a<10; a++)
    {
      //top_io << test;
      test++;

      // write binary
      top_io.write(buf, sizeof(test));

      cout << " tellg=" << top_io.tellg();
      cout << " tellp=" << top_io.tellp() << endl;
    }

    //top_io.flush();

    test = 0;

    // seek beginning
    top_io.seekg(0, ios::beg);

    for (long a = 0; a<10; a++)
    {
      //top_io >> test;

      // read binary
      top_io.read(buf, sizeof(test));

      cout << hex << test;
      // cout << endl;
      cout << dec;
      cout << " tellg=" << top_io.tellg();
      cout << " tellp=" << top_io.tellp() << endl;
    }

  }

  top_io.close();

  cin.get();
  return(0);
}


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,625
  • Joined: 05-May 12

Re: I am not getting the results I want from my program using fstream

Posted 19 September 2012 - 10:15 PM

Yup, so am I. Strange. This is different behavior from a long, long time ago when our teacher had us dissect the fstream and related classes. Must have been a completely different implementation back then for the particular version of C++ we were looking at. It was C++ before templates. :)

Anyway, even if the read and write pointers are synchronized, the operation of writing out on line 9, would advance the both the read and write pointers. For the read operation to read what was just written out, the read pointer would have to be backed up to the position where the write pointer was right before writing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1