PDA

View Full Version : OpenGL, c++, moving text



dsfedorov
12-16-2017, 07:47 AM
I have a task that contains "Make a falling text, which'll be read from txt file". I'm learning c++ for about 2 month, but I have to do this.
I've made a code from a lot of tutorials, but it doesnt work.

#include <windows.h> // Header File For Windows
#include <math.h> // Header File For Windows Math Library ( ADD )
#include <stdio.h> // Header File For Standard Input/Output ( ADD )
#include <stdarg.h> // Header File For Variable Argument Routines ( ADD )
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <GL\glut.h>

HDC hDC = NULL; // Private GDI Device Context
HGLRC hRC = NULL; // Permanent Rendering Context
HWND hWnd = NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

GLuint base; // Base Display List For The Font Set
GLfloat cnt1; // 1st Counter Used To Move Text & For Coloring
GLfloat cnt2; // 2nd Counter Used To Move Text & For Coloring

bool keys[256]; // Array Used For The Keyboard Routine
bool active = TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen = TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc


// Window size
int width = 640;
int height = 480;

GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
base = glGenLists(96); // Storage For 96 Characters ( NEW )
font = font = CreateFont(-14, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, (L"Arial"));
SelectObject(hDC, font); // Выбрать шрифт, созданный нами ( НОВОЕ )
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
}

GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
return; // Do Nothing
va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits ( NEW )
glListBase(base - 32); // Sets The Base Character to 32 ( NEW )
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text ( NEW )
glPopAttrib(); // Pops The Display List Bits ( NEW )
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

BuildFont(); // Build The Font

return TRUE; // Initialization Went OK
}

/*
void Print()
{
// clear (has to be done at the beginning)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

DisplayText(-50, 0, 30.0f, 0.0f, 45.8f, "Tha Hatters!");

// Swap buffers (has to be done at the end)
glutSwapBuffers();
}
*/
/*
void Enable2D(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width / 2.0f, width / 2.0f, -height / 2.0f, height / 2.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
*/
/*
void DisplayText(float x, float y, float r, float g, float b, const char *string)
{
int j = strlen(string);

glColor3f(r, g, b);
glRasterPos2f(x, y);
for (int i = 0; i < j; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]);
}
}
*/

void DrawGLScene() // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f, 0.0f, -1.0f); // Move One Unit Into The Screen
// Pulsing Colors Based On Text Position
glColor3f(1.0f*float(cos(cnt1)), 1.0f*float(sin(cnt2)), 1.0f - 0.5f*float(cos(cnt1 + cnt2)));
// Position The Text On The Screen
glRasterPos2f(-0.45f + 0.05f*float(cos(cnt1)), 0.35f*float(sin(cnt2)));
//Print;
glPrint("TEXT - %7.2f", cnt1); // Print GL Text To The Screen
cnt1 += 0.051f; // Increase The First Counter
cnt2 += 0.005f; // Increase The Second Counter
// return TRUE; // Everything Went OK
}

// Program entry point
int main(int argc, char** argv)
{
// Initialize opengl (via glut)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("RGZ");

glutDisplayFunc(DrawGLScene);

InitGL();

// Start the whole thing
glutMainLoop();
return 0;
}

Or what should I do to get this code moving and reading from file?

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <Windows.h>
#include <fstream>

#include <GL/glut.h>

using namespace std;

void display(void);
void polygon(int a, int b, int c, int d);
void DrawCube();
void printtext(int x, int y, string String);

int WindowHeight = 1000;
int WindowWidth = 1000;


int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WindowWidth, WindowHeight);
glutInitWindowPosition(0, 0);

glutCreateWindow("RGZ");

glutDisplayFunc(display);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 1, 1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(2, 2, 10, 2, 0, 0, 0, 1, 0);

glutMainLoop();
return 0;
}

void printtext()
{
ifstream file;
file.open("program.txt");
if (!file.is_open()) return;

string word;
while (file >> word)
{
cout << word << '\n';
}
}


void printtext(int x, int y, string String)
{
//(x,y) is from the bottom left of the window
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, WindowWidth, 0, WindowHeight, -1.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
glRasterPos2i(x, y);
for (int i = 0; i<String.size(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, String[i]);
}
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);

char string[64];
sprintf_s(string, "lemme do this rgz");
printtext(100, 100, string);

glutSwapBuffers();
}
p.s.Sorry for grammar mistakes, I'm from Russia

didydriver
12-17-2017, 06:42 AM
try making string word; a global and passing it to the other printtext(x,y,string); the overloaded function might be confusing to anyone that uses this other than yourself so make notes in the file about that. If you want it to move you have to make sure the the x&&y variables are defined and have some operation for changing them.


string String;
int x = ?;
int y = ?; << substitute ? with a value;
void printtext()
{
std::ifstrem file("drive/directory/subdirectory/filename.extension",std::ifstream::in); // e.g. c:/myfiles/program.txt << opens the file at initialization
if(!file.open()) { /* file not found or valid error code here; */ }
while(!file.eof()) file>>String;
std::cout<<string<<std::endl;
file.close();
}
inside ( printtext(int x , int y , string String )
{
// your previouse code;
x += || x -= || int operationChangeX(/* overloaded if you wish */)
// do the same for y or just do y
}


Also you can choose to change the variables through another function. Just remember you have to have them global or pass them to the function if they are dynamic.