Tinkering with c++ memory model
This commit is contained in:
parent
41bdca0161
commit
8ea10129ac
14 changed files with 476 additions and 104 deletions
|
@ -15,8 +15,10 @@ add_subdirectory(include)
|
|||
#install(TARGETS cpplab RUNTIME DESTINATION bin)
|
||||
|
||||
|
||||
# CPack instructions for bundling purposes
|
||||
set(CPACK_PACKAGE_CONTACT "Balhau")
|
||||
set(CPACK_GENERATOR "STGZ;TGZ;TZ;DEB")
|
||||
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "CppLab")
|
||||
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
|
||||
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
|
||||
include(CPack)
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
add_subdirectory(memory)
|
||||
add_subdirectory(memory)
|
||||
add_subdirectory(util)
|
63
include/misc/memory/model.h
Normal file
63
include/misc/memory/model.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "../util/debug.h"
|
||||
|
||||
|
||||
template <typename Object> struct SmartPointer{
|
||||
Object *ref;
|
||||
int counter;
|
||||
~SmartPointer(){
|
||||
print("SmartPointer Desctructor called")
|
||||
if(counter==0){
|
||||
delete ref;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Object>
|
||||
using SPointer = struct SmartPointer;
|
||||
|
||||
template <typename Object>
|
||||
class Box
|
||||
{
|
||||
public:
|
||||
Box(Object *); //constructor
|
||||
~Box(); //destructor
|
||||
Box(const Box&); //copy constructor
|
||||
Object *operator->();
|
||||
Object *operator*(); //deref operator
|
||||
int getRefCounter(){
|
||||
return this->sref->counter;
|
||||
};
|
||||
friend std::ostream &operator<<(std::ostream &stream, Box const &c)
|
||||
{
|
||||
return stream << "Boxed[v=" << *c.sref->ref << ",r=" << c.sref->ref << "]";
|
||||
}
|
||||
|
||||
private:
|
||||
SPointer<Object>* sref;
|
||||
};
|
||||
|
||||
template <typename Number>
|
||||
class Point2D
|
||||
{
|
||||
private:
|
||||
Number _x;
|
||||
Number _y;
|
||||
|
||||
public:
|
||||
Point2D(Number x, Number y) : _x(x), _y(y){
|
||||
print("Point2D default constructor called: " << this)}; //Default constructor
|
||||
Point2D(const Point2D &); //Copy constructor
|
||||
Point2D(Point2D &&); //Move constructor
|
||||
~Point2D();
|
||||
//Destructor
|
||||
friend std::ostream &operator<<(std::ostream &stream, Point2D const &c)
|
||||
{
|
||||
return stream << "(" << c._x << "," << c._y << ")";
|
||||
}
|
||||
|
||||
Number getX() { return _x; }
|
||||
Number getY() { return _y; }
|
||||
};
|
|
@ -1,19 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class DummyObject {
|
||||
public:
|
||||
DummyObject(int); //Constructor
|
||||
~DummyObject(); //destructor
|
||||
DummyObject(const DummyObject&); //copy constructor
|
||||
DummyObject(DummyObject&&); //move constructor
|
||||
|
||||
void doSomething();
|
||||
|
||||
|
||||
private:
|
||||
int size;
|
||||
int* list_of_numbers;
|
||||
|
||||
};
|
0
include/misc/util/CMakeLists.txt
Normal file
0
include/misc/util/CMakeLists.txt
Normal file
9
include/misc/util/debug.h
Normal file
9
include/misc/util/debug.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#define DEBUG_MODE
|
||||
|
||||
#ifdef DEBUG_MODE
|
||||
#define print(x) std::cout << x << std::endl;
|
||||
#else
|
||||
#define print(x)
|
||||
#endif
|
|
@ -40,9 +40,9 @@ BMath::Complex BMath::Complex::operator+(const Complex &rightOp)
|
|||
this->imaginary + rightOp.imaginary);
|
||||
};
|
||||
|
||||
BMath::Complex BMath::Complex::operator*(const Complex &rigthOp)
|
||||
BMath::Complex BMath::Complex::operator*(const Complex &rightOp)
|
||||
{
|
||||
return Complex(
|
||||
this->real * rigthOp.real - this->imaginary * rigthOp.imaginary,
|
||||
this->imaginary * rigthOp.real + this->real * rigthOp.imaginary);
|
||||
this->real * rightOp.real - this->imaginary * rightOp.imaginary,
|
||||
this->imaginary * rightOp.real + this->real * rightOp.imaginary);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,2 @@
|
|||
add_executable(
|
||||
spointer
|
||||
smartpointer.cpp
|
||||
)
|
||||
add_executable(memorymodel model.cpp)
|
||||
add_executable(volatile volatile.cpp)
|
||||
|
|
99
src/misc/memory/model.cpp
Normal file
99
src/misc/memory/model.cpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
#include "../../../include/misc/memory/model.h"
|
||||
#include "../../../include/misc/util/debug.h"
|
||||
#include <memory>
|
||||
|
||||
#define UPtr std::unique_ptr
|
||||
#define SPtr std::shared_ptr
|
||||
|
||||
template <typename Object>
|
||||
Box<Object>::Box(Object *ref)
|
||||
{
|
||||
this->sref = new SPointer<Object>{ref,0};
|
||||
this->sref->ref=ref;
|
||||
print("Box constructor called: " << this)
|
||||
}
|
||||
|
||||
template <typename Object>
|
||||
Box<Object>::Box(const Box &other){
|
||||
print("Box Copy constructor: " << this)
|
||||
this->sref=new SPointer<Object>{other.sref->ref,other.sref->counter+1};
|
||||
}
|
||||
|
||||
template <typename Object>
|
||||
Object* Box<Object>::operator->()
|
||||
{
|
||||
return this->ref;
|
||||
}
|
||||
|
||||
template <typename Object>
|
||||
Object* Box<Object>::operator*(){
|
||||
return this->sref->ref;
|
||||
}
|
||||
|
||||
template <typename Object>
|
||||
Box<Object>::~Box()
|
||||
{
|
||||
print("Box Destructor called: " << this) delete this->sref;
|
||||
}
|
||||
|
||||
//Copy constructor
|
||||
template <typename Number>
|
||||
Point2D<Number>::Point2D(const Point2D<Number> &other)
|
||||
{
|
||||
print("Point2D copy constructor called: " << this) this->_x = other._x;
|
||||
this->_y = other._y;
|
||||
}
|
||||
//Move constructor
|
||||
template <typename Number>
|
||||
Point2D<Number>::Point2D(Point2D<Number> &&other)
|
||||
{
|
||||
print("Point2D move constructor called: " << this)
|
||||
}
|
||||
|
||||
//Destructor
|
||||
template <typename Number>
|
||||
Point2D<Number>::~Point2D()
|
||||
{
|
||||
print("Point2D destructor called: " << this)
|
||||
}
|
||||
|
||||
template <typename Number>
|
||||
void printPointByValue(Point2D<Number> point)
|
||||
{
|
||||
print(point << " : " << &point)
|
||||
}
|
||||
|
||||
template <typename Number>
|
||||
void printPointByRef(Point2D<Number> *point){
|
||||
print(*point << ": " << point)
|
||||
}
|
||||
|
||||
template <typename Object>
|
||||
void printBoxContent(Box<Object> box){
|
||||
print("Box: Content: " << *box << ":" << box << ":" << box.getRefCounter())
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
print("Stack memory management")
|
||||
Point2D<int> p1={1,2};
|
||||
printPointByValue(p1);
|
||||
print("")
|
||||
|
||||
print("Heap memory management")
|
||||
Point2D<int> *p2 = new Point2D<int>{3, 4};
|
||||
printPointByRef(p2);
|
||||
delete p2;
|
||||
print("")
|
||||
|
||||
print("Heap/Stack memory management")
|
||||
Point2D<int> *ptrPt3 = new Point2D<int>(10,20);
|
||||
Box<Point2D<int>> box1(ptrPt3);
|
||||
print(box1)
|
||||
print("")print("")
|
||||
|
||||
printBoxContent(box1);
|
||||
Box<Point2D<int>> box2=box1;
|
||||
printBoxContent(box2);
|
||||
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
#include "../../../include/misc/memory/smartpointer.h"
|
||||
#include <memory>
|
||||
|
||||
#define UPtr std::unique_ptr
|
||||
#define SPtr std::shared_ptr
|
||||
|
||||
DummyObject::DummyObject(int num_elements)
|
||||
{
|
||||
std::cout << "Constructor called " << num_elements << std::endl;
|
||||
size = num_elements;
|
||||
list_of_numbers = new int[size];
|
||||
}
|
||||
|
||||
DummyObject::~DummyObject()
|
||||
{
|
||||
std::cout << "Destructor called " << size << std::endl;
|
||||
delete[] list_of_numbers;
|
||||
}
|
||||
|
||||
DummyObject::DummyObject(const DummyObject &other)
|
||||
{
|
||||
std::cout << "Copy constructor called " << other.size << std::endl;
|
||||
list_of_numbers = new int[other.size];
|
||||
for (int i = 0; i < other.size; i++)
|
||||
{
|
||||
list_of_numbers[i] = other.list_of_numbers[i];
|
||||
}
|
||||
size = other.size;
|
||||
}
|
||||
|
||||
DummyObject::DummyObject(DummyObject&& moved)
|
||||
{
|
||||
std::cout << "Move constructor called " << moved.size << std::endl;
|
||||
}
|
||||
|
||||
void DummyObject::doSomething()
|
||||
{
|
||||
std::cout << "This do amazing stuff: " << size << std::endl;
|
||||
}
|
||||
|
||||
DummyObject createDummy(int size)
|
||||
{
|
||||
DummyObject d(size);
|
||||
return d;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
std::cout << "Smart pointer examples" << std::endl;
|
||||
|
||||
DummyObject d(15);
|
||||
DummyObject copy = d;
|
||||
|
||||
DummyObject *dmtPtr = new DummyObject(20);
|
||||
UPtr<DummyObject> dmySmtPtr = UPtr<DummyObject>(new DummyObject(30));
|
||||
|
||||
dmySmtPtr->doSomething();
|
||||
dmtPtr->doSomething();
|
||||
|
||||
SPtr<DummyObject> firstSmtPtr = SPtr<DummyObject>(dmtPtr);
|
||||
SPtr<DummyObject> secondSmtPtr = firstSmtPtr;
|
||||
|
||||
DummyObject retDummy = createDummy(32);
|
||||
DummyObject secondDummy = std::move(retDummy);
|
||||
|
||||
retDummy.doSomething();
|
||||
secondDummy.doSomething();
|
||||
|
||||
firstSmtPtr->doSomething();
|
||||
secondSmtPtr->doSomething();
|
||||
}
|
22
src/misc/memory/volatile.cpp
Normal file
22
src/misc/memory/volatile.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
|
||||
inline unsigned long getTimestamp(){
|
||||
unsigned hi,lo;
|
||||
asm ("rdtsc": "=a" (lo), "=d" (hi) );
|
||||
return ((unsigned long) lo )| ((unsigned long) hi << 32);
|
||||
}
|
||||
|
||||
inline unsigned long getTimestampVolatile(){
|
||||
unsigned hi,lo;
|
||||
asm volatile("rdtsc": "=a" (lo), "=d" (hi) );
|
||||
return ((unsigned long) lo )| ((unsigned long) hi << 32);
|
||||
}
|
||||
|
||||
int main(void){
|
||||
unsigned long t1=getTimestamp();
|
||||
unsigned long t2=getTimestamp();
|
||||
std::cout << "Hello 1 -- " << t1 <<std::endl;
|
||||
std::cout << "Hello 2 -- " << t2 <<std::endl;
|
||||
std::cout << "Hello 3 -- " << getTimestamp() <<std::endl;
|
||||
std::cout << "Hello 4 -- " << getTimestamp() <<std::endl;
|
||||
}
|
|
@ -23,11 +23,11 @@ add_executable(
|
|||
utils.cpp
|
||||
)
|
||||
|
||||
add_executable(gl2 gl2.cpp)
|
||||
target_link_libraries(gl2 ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
|
||||
|
||||
add_executable(gl3 gl3.cpp)
|
||||
target_link_libraries(gl3 ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
|
||||
|
||||
# Link gl1 with glfw lib
|
||||
target_link_libraries(gl1 glfw)
|
||||
# Link gl1 with glew
|
||||
target_link_libraries(gl1 GLEW)
|
||||
# Link gl1 with OpenGL
|
||||
target_link_libraries(gl1 OpenGL::GL)
|
||||
#target_link_libraries(gl1 ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
|
||||
target_link_libraries(gl1 glfw GLEW OpenGL::GL)
|
147
src/opengl/gl2.cpp
Normal file
147
src/opengl/gl2.cpp
Normal file
|
@ -0,0 +1,147 @@
|
|||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glut.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PT 1
|
||||
#define STROKE 2
|
||||
#define END 3
|
||||
|
||||
typedef struct charpoint
|
||||
{
|
||||
GLfloat x, y;
|
||||
int type;
|
||||
} CP;
|
||||
|
||||
CP Adata[] = {
|
||||
{0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}};
|
||||
|
||||
CP Edata[] = {
|
||||
{5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE}, {0, 5, PT}, {4, 5, END}};
|
||||
|
||||
CP Pdata[] = {
|
||||
{0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, {4, 5, PT}, {0, 5, END}};
|
||||
|
||||
CP Rdata[] = {
|
||||
{0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}};
|
||||
|
||||
CP Sdata[] = {
|
||||
{0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT}, {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, {5, 9, END}};
|
||||
|
||||
/* drawLetter() interprets the instructions from the array
|
||||
* for that letter and renders the letter with line segments.
|
||||
*/
|
||||
static void drawLetter(CP *l)
|
||||
{
|
||||
glBegin(GL_LINE_STRIP);
|
||||
while (1)
|
||||
{
|
||||
switch (l->type)
|
||||
{
|
||||
case PT:
|
||||
glVertex2fv(&l->x);
|
||||
break;
|
||||
case STROKE:
|
||||
glVertex2fv(&l->x);
|
||||
glEnd();
|
||||
glBegin(GL_LINE_STRIP);
|
||||
break;
|
||||
case END:
|
||||
glVertex2fv(&l->x);
|
||||
glEnd();
|
||||
glTranslatef(8.0, 0.0, 0.0);
|
||||
return;
|
||||
}
|
||||
l++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create a display list for each of 6 characters */
|
||||
static void init(void)
|
||||
{
|
||||
GLuint base;
|
||||
|
||||
glShadeModel(GL_FLAT);
|
||||
|
||||
base = glGenLists(128);
|
||||
glListBase(base);
|
||||
glNewList(base + 'A', GL_COMPILE);
|
||||
drawLetter(Adata);
|
||||
glEndList();
|
||||
glNewList(base + 'E', GL_COMPILE);
|
||||
drawLetter(Edata);
|
||||
glEndList();
|
||||
glNewList(base + 'P', GL_COMPILE);
|
||||
drawLetter(Pdata);
|
||||
glEndList();
|
||||
glNewList(base + 'R', GL_COMPILE);
|
||||
drawLetter(Rdata);
|
||||
glEndList();
|
||||
glNewList(base + 'S', GL_COMPILE);
|
||||
drawLetter(Sdata);
|
||||
glEndList();
|
||||
glNewList(base + ' ', GL_COMPILE);
|
||||
glTranslatef(8.0, 0.0, 0.0);
|
||||
glEndList();
|
||||
}
|
||||
|
||||
const char *test1 = "A SPARE SERAPE APPEARS AS";
|
||||
const char *test2 = "APES PREPARE RARE PEPPERS";
|
||||
|
||||
static void printStrokedString(char *s)
|
||||
{
|
||||
GLsizei len = strlen(s);
|
||||
glCallLists(len, GL_BYTE, (GLbyte *)s);
|
||||
}
|
||||
|
||||
void display(void)
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glColor3f(1.0, 1.0, 1.0);
|
||||
glPushMatrix();
|
||||
glScalef(2.0, 2.0, 2.0);
|
||||
glTranslatef(10.0, 30.0, 0.0);
|
||||
printStrokedString((char *)test1);
|
||||
glPopMatrix();
|
||||
glPushMatrix();
|
||||
glScalef(2.0, 2.0, 2.0);
|
||||
glTranslatef(10.0, 13.0, 0.0);
|
||||
printStrokedString((char *)test2);
|
||||
glPopMatrix();
|
||||
glFlush();
|
||||
}
|
||||
|
||||
void reshape(int w, int h)
|
||||
{
|
||||
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int x, int y)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case ' ':
|
||||
glutPostRedisplay();
|
||||
break;
|
||||
case 27:
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
|
||||
glutInitWindowSize(440, 120);
|
||||
glutCreateWindow(argv[0]);
|
||||
init();
|
||||
glutReshapeFunc(reshape);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutDisplayFunc(display);
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
}
|
121
src/opengl/gl3.cpp
Normal file
121
src/opengl/gl3.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glut.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
GLubyte space[] =
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
GLubyte letters[][13] = {
|
||||
{0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18},
|
||||
{0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
|
||||
{0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e},
|
||||
{0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc},
|
||||
{0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff},
|
||||
{0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff},
|
||||
{0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e},
|
||||
{0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
|
||||
{0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e},
|
||||
{0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06},
|
||||
{0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6, 0xc3},
|
||||
{0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0},
|
||||
{0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xdb, 0xff, 0xff, 0xe7, 0xc3},
|
||||
{0x00, 0x00, 0xc7, 0xc7, 0xcf, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3},
|
||||
{0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e},
|
||||
{0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
|
||||
{0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c},
|
||||
{0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
|
||||
{0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e},
|
||||
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff},
|
||||
{0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
|
||||
{0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
|
||||
{0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
|
||||
{0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3},
|
||||
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3},
|
||||
{0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}};
|
||||
|
||||
GLuint fontOffset;
|
||||
|
||||
void makeRasterFont(void)
|
||||
{
|
||||
GLuint i, j;
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
fontOffset = glGenLists(128);
|
||||
for (i = 0,j = 'A'; i < 26; i++,j++) {
|
||||
glNewList(fontOffset + j, GL_COMPILE);
|
||||
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
|
||||
glEndList();
|
||||
}
|
||||
glNewList(fontOffset + ' ', GL_COMPILE);
|
||||
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
|
||||
glEndList();
|
||||
}
|
||||
|
||||
void init(void)
|
||||
{
|
||||
glShadeModel(GL_FLAT);
|
||||
makeRasterFont();
|
||||
}
|
||||
|
||||
void printString(char *s)
|
||||
{
|
||||
glPushAttrib(GL_LIST_BIT);
|
||||
glListBase(fontOffset);
|
||||
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *)s);
|
||||
glPopAttrib();
|
||||
}
|
||||
|
||||
/* Everything above this line could be in a library
|
||||
* that defines a font. To make it work, you've got
|
||||
* to call makeRasterFont() before you start making
|
||||
* calls to printString().
|
||||
*/
|
||||
void display(void)
|
||||
{
|
||||
GLfloat white[3] = {1.0, 1.0, 1.0};
|
||||
const char* p1="LINDA MERDA";
|
||||
const char* p2="PUTA MERDA";
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glColor3fv(white);
|
||||
|
||||
glRasterPos2i(20, 60);
|
||||
printString((char*)p1);
|
||||
glRasterPos2i(20, 40);
|
||||
printString((char*)p2);
|
||||
glFlush();
|
||||
}
|
||||
|
||||
void reshape(int w, int h)
|
||||
{
|
||||
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0, w, 0.0, h, -1.0, 1.0);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int x, int y)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case 27:
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
|
||||
glutInitWindowSize(300, 100);
|
||||
glutInitWindowPosition(100, 100);
|
||||
glutCreateWindow(argv[0]);
|
||||
init();
|
||||
glutReshapeFunc(reshape);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutDisplayFunc(display);
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue