Display window will not show up

I am trying to load a ppm file using the code below. The program compiles but the display window does not show up at all. I am using codeblocks. If I comment out the first part of the main function the display window shows a blank white screen, but without commenting the display window does not show at all. I don't know what is causing the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include<GL/gl.h>
#include<GL/glut.h>
#include<windows.h>  


int n;
int m;

int *image;

void myInit (void)
{
     glClearColor ( 1.0, 1.0, 1.0 , 1.0);
     glColor3f ( 1.0f, 0.0f, 0.0f );
     glPointSize ( 4.0 );
     glMatrixMode ( GL_PROJECTION );
     glLoadIdentity ( );
     gluOrtho2D ( 0.0, 400.0, 1.0, 400.0 );
}


void display(void)
{

	glClear ( GL_COLOR_BUFFER_BIT );
	glRasterPos2i(0,0);
	glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image);
      glFlush ( );

}

void myreshape(int h, int w)
{
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glViewport(0,0,h,w);
}

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

	FILE *fd;
	int k, nm;
	char c;
	char b[70];
	float s;
	char red, green, blue;
	int x, y;

	fd = fopen("map.ppm", "r");
	if(fd == 0)
	{
		exit(0);
	}

	fscanf(fd, "%s", b);
	if((b[0] != 'P') || (b[1] != '6'))
	{
		printf("%s is not a PPM file!\n", b);
		exit(0);
	}

	fscanf(fd, "%c", &c);

	fscanf(fd, "%c", &c);
	while(c == '#')
	{
		fscanf(fd, "%[^\n]", b);
		printf("%s\n", b);
		fscanf(fd, "%c", &c);
		printf("%c", c);
	}

	ungetc(c,fd);

	fscanf(fd, "%d %d %d", &n, &m, &k);

	printf("%d rows  %d colums  max value = %d\n", n, m, k);

	nm = n*m;

	image = (int*)malloc(3*sizeof(GLint)*nm);

	s = 255./k;

	for(x = 0; x < m; x++)
	{
		for(y = n-1; y >= 0; y--)
		{
           fscanf(fd, "%c", &red);
    	   fscanf(fd, "%c", &green);
	       fscanf(fd, "%c", &blue);

		   image[3*nm - 3*(x*n +y) -3] = green;
		   image[3*nm - 3*(x*n +y) -2] = blue;
		   image[3*nm - 3*(x*n +y) -1] = red;

		}
	}


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(n,m);
	glutInitWindowPosition(0,0);
	glutCreateWindow("image");
	glutReshapeFunc(myreshape);
	glutDisplayFunc(display);
	glPixelTransferf(GL_RED_SCALE, s);
	glPixelTransferf(GL_GREEN_SCALE, s);
	glPixelTransferf(GL_BLUE_SCALE, s);
	glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
	glClearColor(1.0, 1.0, 1.0, 1.0);
	myInit ( );
      glutMainLoop ( );
}


Put map.ppm in the same folder as your Code::Blocks project *.cbp file directory if you execute the EXE via CodeBlocks, this is the working directory.

Or put it in the same folder as the EXE, but ONLY execute it by double-clicking directy from Windows Explorer.
Putting it in the same folder as the EXE and double clicking the application from Windows Explorer worked.

Does this mean that if I want to add objects or modify the program with that background, I will have to execute it by clicking it from Windows Explorer every time? Is there a work around for that?

Thank you for your help!!
Yes, there is. To make work from inside CodeBlocks use SetCurrentDirectory() API in your code.

To find out executable directory use GetModuleFileName() and PathRemoveFileSpec() APIs. All resources (images or whatever) must still be in the same folder as the EXE, but this workaround will help you for running the EXE from CodeBlocks directly.



It could be possible to change CodeBlocks settings, but I could not help you in that matter.
Last edited on
Registered users can post here. Sign in or register to post.