LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2012, 09:48 AM   #1
924199952
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Import a txt file and read line by line, polyline by polyline C++


Here's my code:

/**************************************************************
* GameTemplate-1.cpp
*
* This program demonstrates how to do some basic stuff using mouse
*
* Use left mouse button start animation.
* Use right mouse botton to exit.
*cl -I. -D WIN32 Test.cpp opengl32.lib glut32.lib glu32.lib
* 2010 at VSU
**************************************************************/

#include <glut.h> //must be included for OpenGL
#include <gl\gl.h> //must be included for OpenGL

#include <time.h> //must be included for time functions
#include <iostream> //must be included for console input/output
#include <fstream>
using namespace std;

#define WINDOW_WID 800
#define WINDOW_HEI 600


double MAPX[1000];
double MAPY[1000];
int N_MAP_POINTS;
/////////////////////////////////////////

void myInit(void)
{

glClearColor (1.0, 1.0, 1.0, 0.0); // set background color to black
glShadeModel (GL_FLAT);

ifstream infile;
infile.open ("dino.dat");

if(!infile)
{
cout << "open file failed\n";
exit(0);
}

N_MAP_POINTS=0;
while(!infile.eof())
{
infile >> MAPX[N_MAP_POINTS] >> MAPY[N_MAP_POINTS];
N_MAP_POINTS++;
}
infile.close();
}

void ShowUSMAP()
{
glColor3f(1,0,0); //set the drawing color
glLineWidth(1); //set the point size
glBegin(GL_LINE_LOOP);
//glBegin(GL_POLYGON);
//glVertex2d(randomPx,randomPy); //Set the position of the vertex
for(int i=0;i<N_MAP_POINTS;i++)
glVertex2d(MAPX[i],MAPY[i]);////////////////////////*********
glEnd();
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

ShowUSMAP();

glutSwapBuffers (); //put everything on your screen
}


void myReshape ( int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION); // set "camera type"
glLoadIdentity (); // clear the matrix

glOrtho(0.0, w, 0.0, h, -1.0, 1.0); // viewing transformation
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

void Animation()
{
srand(time(NULL)+rand()); //set the seed for your rand function
randomPx = rand()%WINDOW_WID;
randomPy = rand()%WINDOW_HEI;
Sleep(200); //put the program to sleep for 200 ms

myDisplay();
}

void myMouse(int button, int state, int x, int y)
{
y = WINDOW_HEI - y;
switch (button)
{
case GLUT_LEFT_BUTTON: //when left mouse button is clicked
if (state == GLUT_DOWN)
{
cout << "When mouse is up, animation starts\n";
}
else if(state == GLUT_UP)
{
glutIdleFunc(Animation); //do Animation when idle
}
break;
case GLUT_RIGHT_BUTTON: //when right mouse button is clicked
if (state == GLUT_DOWN)
{
glutIdleFunc(NULL); //do nothing when idle
}
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
{
exit (-1); //exit your program
}
break;
}
myDisplay();
}

void myMotion(int x, int y)
{
y = WINDOW_HEI - y;


myDisplay();
}

void myKeyboard(unsigned char key, int x, int y)
{
//TODO

myDisplay();
}

/*
* Request double buffer display mode.
* Register mouse input callback functions
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); // set display mode
// glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize (WINDOW_WID, WINDOW_HEI); // set screen window size
glutInitWindowPosition (100, 100); // set window position on screen
glutCreateWindow (argv[0]); // open the screen window
myInit ();
glutDisplayFunc(myDisplay); // register redraw function
glutReshapeFunc(myReshape); // register reshape function

glutMouseFunc(myMouse); //GLUT provides a way for you to register the function that will be responsable for processing events generated by mouse clicks.
glutMotionFunc(myMotion); //There are two types of motion that GLUT handles: active and passive motion. Active motion occurs when the mouse is moved and a button is pressed.
glutKeyboardFunc(myKeyboard);
//glutPassiveMotionFunc(myPassiveMotion); //Passive motion is when the mouse is moving but no buttons are pressed. If an application is tracking motion, an event will be generated per frame during the period that the mouse is moving.
//glutEntryFunc(processMouseEntry); //GLUT is also able to detect when the mouse leaves or enters the window region. A callback function can be registered to handle these two events.

glutMainLoop(); // go into a perpetual loop
return 0;
}

////////////////////////////////////////////////
WHAT I WANT??

I want my code to draw multiple polylines from this file below:

21 << this is how many lines are in file
29 << this represents how many points for the first <<line
32 435
10 439
4 438
2 433
4 428
6 425
10 420
15 416
21 413
30 408
42 406
47 403
56 398
63 391
71 383
79 369
84 356
87 337
89 316
88 302
86 294
83 278
79 256
78 235
79 220
85 204
94 190
98 183
98 182
9
116 189
105 184
98 172
98 156
93 141
93 132
99 122
104 115
104 114
38 <<The next 38 points are one line etc etc
153 116
152 112
153 107
154 93
154 81
152 67
146 56
140 47
136 39
133 30
130 17
128 7
127 3
93 2
93 10
96 16
96 20
97 21
101 24
104 27
105 31
107 36
108 40
109 47
111 51
114 58
118 66
120 71
118 79
117 88
116 97
112 105
107 113
107 118
108 126
112 138
116 146
118 148
7
153 95
158 99
159 103
161 108
161 115
160 121
160 122
7
156 80
167 79
182 76
203 73
220 78
235 79
239 80
43
262 154
259 141
259 123
257 110
255 93
259 86
272 74
287 46
290 41
299 30
305 21
307 15
307 12
300 11
299 9
301 2
303 1
313 5
320 7
307 1
312 0
321 0
325 0
331 0
336 2
336 8
334 18
335 24
331 29
327 39
323 45
317 54
312 63
308 70
301 79
297 86
296 97
300 109
303 120
304 126
307 138
306 148
305 152
5
298 86
304 92
310 104
314 114
314 119
7
255 98
251 100
246 105
242 112
236 122
231 131
233 126
7
271 73
264 74
257 76
244 76
236 80
231 84
230 86
24
242 77
242 70
245 61
246 49
248 39
249 30
248 19
245 12
242 9
241 6
243 1
256 3
259 2
266 3
271 4
274 9
277 16
277 24
277 31
277 41
277 48
278 57
278 62
278 66
24
190 73
191 64
193 51
194 39
191 25
189 17
185 7
184 5
177 4
169 4
166 4
159 5
159 6
162 19
163 25
165 32
165 39
165 47
165 57
162 65
161 70
159 75
158 78
157 80
15
96 157
93 163
88 176
82 184
78 193
75 201
72 212
72 224
72 238
73 254
75 267
78 277
82 286
89 298
89 300
9
33 428
33 428
33 427
35 426
36 425
30 427
27 428
27 428
28 428
16
2 435
5 434
10 432
13 431
16 429
19 427
21 426
22 425
24 424
26 423
27 423
30 422
33 422
34 421
36 420
36 419
77
32 436
55 423
67 415
75 409
86 401
92 395
98 389
105 378
107 372
111 362
112 355
116 345
119 338
121 328
124 317
125 312
125 304
126 294
125 288
124 280
125 277
125 258
124 255
125 241
128 235
135 225
141 218
147 211
155 208
166 205
178 203
194 202
209 203
219 204
232 205
249 206
259 207
284 205
300 198
317 189
333 182
345 170
362 153
392 135
430 118
450 104
477 91
509 75
539 65
567 61
599 60
625 59
635 58
632 54
616 51
602 46
593 46
580 45
565 41
546 38
526 36
502 42
487 48
468 53
452 57
434 63
414 71
397 77
378 82
366 86
352 90
338 92
328 91
319 88
307 86
306 85
301 85
14
318 106
333 107
346 109
359 111
369 104
391 100
411 95
431 87
445 81
458 71
473 63
491 59
497 57
499 56
7
244 109
235 104
221 100
208 96
199 97
190 98
190 98
8
160 116
165 119
171 122
172 127
170 135
168 144
170 149
174 149
4
169 118
174 120
179 124
178 126
8
293 132
294 125
297 115
291 94
287 90
290 84
297 79
297 79
6
144 97
143 83
144 72
141 58
136 52
134 49
 
Old 02-15-2012, 10:02 AM   #2
924199952
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
FYI

I'm using OpenGL in Visual Studio
 
Old 02-16-2012, 05:35 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
so, what's the problem? For example, you could create a class like this (not tested, will probably contain bugs/typos)

Code:
#include <vector>
#include <fstream>
#include <GL/gl.h>

typedef enum {
	readError
} error;

class polyline {
	std::vector < GLint > m_points;
	size_t m_npoints;
public:
	
	polyline() : m_npoints(0) {}

	polyline(std::ifstream& ifs) { ifs >> (*this); }

	friend std::ifstream& operator >> (std::ifstream& ifs, polyline& p) {
		if (!(ifs >> m_npoints)) { throw readError; }
		m_points.reserve(2*npoints);
		for (size_t i = 0; i < 2 * npoints; i++) {
			if(!(ifs >> m_points[i])) { throw readError; }
		}
	}

	void render () const {
		glBegin(GL_LINE_STRIP);
		for (size_t i = 0; i < m_npoints; i++) {
			glVertex2i(m_points[2 * i], m_points[2 * i + 1]);
		}
		glEnd();
	}

};

class polylineSet {
	std::vector <polyline> m_lines;
public:
	polylineSet() { // TODO }
	polylineSet(std::ifstream& ifs) { // TODO }
	polylineSet(const char* filename) { // TODO }

	void render() { // TODO }
};

Last edited by millgates; 02-16-2012 at 07:47 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration