Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "painter.h"
- namespace LIB {
- Painter::Painter() :
- ResMan<sf::Texture>() {
- g_renderer = nullptr;
- // a_renderer = nullptr;
- tex_witch = nullptr;
- tex_tiles = nullptr;
- }
- Painter::~Painter() {
- unloadAll();
- }
- bool Painter::init(CORE::Renderer* ren) {
- g_renderer = ren;
- /*
- if((a_renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED)) == nullptr) {
- printf("Unable to create renderer");
- return false;
- }*/
- tex_witch = aquire("data/gfx/witch.png");
- tex_tiles = aquire("tex.png");
- return true;
- }
- void Painter::cleanup() {
- /*
- if(a_renderer) {
- SDL_DestroyRenderer(a_renderer);
- a_renderer = NULL;
- }
- */
- }
- sf::Texture* Painter::load(const std::string& path) {
- sf::Texture* tex = new sf::Texture;
- if(!tex->loadFromFile(path.c_str())) {
- printf("Unable to load image(s)");
- return nullptr;
- }
- ///sf::Texture::bind(&texture);
- ///sf::Texture::bind(NULL);
- return tex;
- }
- // Removes the textures
- void Painter::unload(const std::string& path) {
- sf::Texture* tex = find(path);
- if(tex!=0) {
- delete tex;
- map_files.erase(map_files.find(path));
- }
- }
- //Removes all textures
- void Painter::unloadAll() {
- while(map_files.begin()!=map_files.end()) {
- delete map_files.begin()->second;
- map_files.erase(map_files.begin());
- }
- }
- void Painter::paint(double ox, double oy, int w, int h, int mx, int my) {
- mx = sf::Mouse::getPosition().x;
- my = sf::Mouse::getPosition().y;
- //glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
- //glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, w, h, 0, -h, h);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- ///xxxxxxxxxxxxxxxx
- ///GLuint bufferID; //x
- //GLuint VertexVBOID; //x
- GLuint IndexVBOID; //x
- glGenBuffers(1, &IndexVBOID);
- glm::vec3 right(1.0, 0.0, 0.0);
- glm::vec3 up(0.0, 1.0, 0.0);
- glm::vec3 position = glm::vec3( 0, 0, 0 );
- glm::vec3 plysize = glm::vec3( 20.0/32.0, 20.0/32.0, -28.0/24.0 );
- glm::vec3 plypos = glm::vec3( 5.0, 5.0, plysize.z+2.0 );
- float speed = 7.3f; // units / second
- float camdir = 0.0;
- float camdir_ang = camdir*180.0/3.1415;
- ///xxxxxxxxxxxxxxxx
- LinkedList<CORE::Draw> draw_list;
- g_renderer->fill_draw_list(draw_list);
- for( auto it = draw_list.first; it != 0; it = it->next ) {
- switch(it->val->get_draw_kind()) {
- case CORE::Draw::D_MODEL:
- CORE::Model* m = (CORE::Model*)it->val;
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(-ox, -oy, 0);
- //MODEL BEGIN
- ///////////////////
- int dat_len;
- float* data = m->get_verts(dat_len);
- if(dat_len==0) break;
- GLfloat* modeldata = new GLfloat[dat_len];
- for( int i=0; i<dat_len; ++i ) modeldata[i] = data[i];
- // Culling
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- // Tex repeat
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // Light
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- ///if(m->shape!=nullptr) break;
- ///GLfloat mat_shininess[] = { 50.0, 50.0, 50.0, 1.0 };;
- ///GLfloat light_position[] = { GLfloat(fmax(0.0f, ox+512)), GLfloat(fmax(0.0f, oy+512)), 100.0f, 1.0f }; //ok
- GLfloat light_position[] = { GLfloat(fmax(0.0f, ox+mx)), GLfloat(fmax(0.0f, oy+my)), 60.0f, 1.0f };
- //glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
- glLightfv(GL_LIGHT0, GL_SPECULAR, mat_specular);
- //glLightfv(GL_LIGHT0, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHT0);
- //glEnable(GL_DEPTH_TEST);
- // Enable vertex components
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glVertexPointer( 3, GL_FLOAT, 11 * sizeof(GLfloat), modeldata);
- glTexCoordPointer( 2, GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 3);
- glColorPointer( 3, GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 5);
- glNormalPointer( GL_FLOAT, 11 * sizeof(GLfloat), modeldata + 8);
- sf::Texture::bind(tex_tiles);
- //sf::Texture::bind(nullptr);
- glDrawArrays(GL_TRIANGLES, 0, dat_len/11);
- ///////////////////
- delete [] modeldata;
- //MODEL END
- //WIREFAME BEGIN
- ///////////////////
- int wire_len;
- float* wireframe = m->get_wire(wire_len);
- if(wire_len==0) break;
- GLfloat* wireframedata = new GLfloat[wire_len];
- for( int i=0; i<wire_len; ++i ) wireframedata[i] = wireframe[i];
- glDisable(GL_CULL_FACE);
- glDisable(GL_LIGHTING);
- glDisable(GL_COLOR_MATERIAL);
- glDisable(GL_LIGHT0);
- // Enable vertex components
- glEnableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- glVertexPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), wireframedata);
- glColorPointer( 3, GL_FLOAT, 6 * sizeof(GLfloat), wireframedata + 3);
- //sf::Texture::bind(nullptr);
- //sf::Texture::bind(nullptr);
- glDrawArrays(GL_LINES, 0, wire_len/6);
- ///////////////////
- delete [] wireframedata;
- //WIREFAME END
- break;
- }
- }
- //SDL_RenderPresent(a_renderer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement