<commit_before>package phoenix;

import lime.gl.GL;
import lime.utils.Libs;

import phoenix.ResourceManager;
import phoenix.geometry.Geometry;
    //default resources
import phoenix.defaults.Shaders;
import phoenix.defaults.FontString;
import phoenix.defaults.FontBytes;

import phoenix.Shader;
import phoenix.Color;
import phoenix.Camera;
import phoenix.Texture;
import phoenix.BitmapFont;

import lime.utils.UInt8Array;
import lime.utils.ArrayBuffer;

import phoenix.utils.BinarySearchTree;

class RendererStats {
    public function new(){}
    public var batchers : Int = 0;
    public var geometry_count : Int = 0;
    public var dynamic_batched_count : Int = 0;
    public var static_batched_count : Int = 0;
    public var enabled_count : Int = 0;
    public var draw_calls : Int = 0;
    public var group_count : Int = 0;
    public function toString() {
        return 
            'Renderer Statistics\n' + 
            '\tbatcher count : ' + batchers + '\n' +
            '\ttotal geometry : ' + geometry_count + '\n' +
            '\tenabled geometry : ' + enabled_count + '\n' +
            '\tdynamic batched geometry : ' + dynamic_batched_count + '\n' +
            '\tstatic batched geometry : ' + static_batched_count + '\n' +
            '\ttotal draw calls : ' + draw_calls;
    }
}

class Renderer {

    public var resource_manager : ResourceManager;
    public var batchers : BinarySearchTree<Batcher>;
        
        //Default rendering
    public var default_shader : Shader;
    public var default_shader_textured : Shader;
        //Default view and batching renderer
    public var default_batcher : Batcher;
    public var default_camera : Camera;
        //Default font for debug stuff etc
    public var default_font : BitmapFont;    

    public var should_clear : Bool = true;
    public var stop : Bool = false;
    public var stop_count : Int = 0;
    public var clear_color : Color;

    public var stats : RendererStats;   

    public function startup() {

        clear_color = new Color(0,0,0,1);
        stats = new RendererStats();
        

        resource_manager = new ResourceManager();
        batchers = new BinarySearchTree<Batcher>( function(a:Batcher,b:Batcher){
            return a.compare(b);
        } );

            //The default view
        default_camera = new Camera( ProjectionType.ortho, { x2 : Lab.screen.w, y2 : Lab.screen.h } );

            //create the default rendering shader
        default_shader = new Shader( resource_manager );  
        default_shader.id = 'default_shader';

        default_shader_textured = new Shader( resource_manager );  
        default_shader_textured.id = 'default_shader_textured';

        default_shader.load_from_string( Shaders.vertex_shader(), 
                                         Shaders.fragment_untextured(), false );

        default_shader_textured.load_from_string( Shaders.vertex_shader(), 
                                                  Shaders.fragment_textured(), false );

            //create the default batcher
        default_batcher = new Batcher( this, 'default_batcher' );
        add_batch(default_batcher);

            //create the default font
        default_font = new BitmapFont( resource_manager );

        trace("Creating the default font...");
            //create the font texture                    
        var _font_texture = load_texture_from_string_byte_array('default_font', FontBytes.data(), 512,512 );
        default_font.load_from_string( FontString.data(), 'phoenix.internal_data.default_font', null, [_font_texture] );

        trace("Done. " + _font_texture.width + 'x' + _font_texture.height );

            //enable z buffer use
        GL.enable(GL.DEPTH_TEST);
            // Accept fragment if it closer or equal away from the other
        GL.depthFunc(GL.LEQUAL);
            //Clear the depth buffer
        GL.clearDepth(1.0);

            //Enable blending
        GL.enable(GL.BLEND);
        GL.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);

            //Make sure that we aren't premultiplying the backbuffer
        #if lime_html5
            GL.pixelStorei(GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
        #end //html5
        
        // trace(':: phoenix :: renderer starting up');        
    }

    public function shutdown() {
        // trace(':: phoenix :: renderer shutting down');   
    }

    public function add_batch(batch:Batcher) {
        batchers.insert(batch);
    }

    public function clear( _color:Color ) {
        if(_color == null) _color = clear_color;

        GL.clearColor( _color.r, _color.g, _color.b, _color.a );
        GL.clear( GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT );
    }


    public function load_texture_from_string_byte_array(_name:String = 'untitled texture', _string_byte_array:String, _width:Int, _height:Int) : Texture {
        
        var _exists = resource_manager.find_texture(_name);

        if(_exists != null) {
            return _exists;
        }

        var _array_data = _string_byte_array.split(' ');
        var _int_array_data : Array<Int> = [];
            //loop over the string items and parse them to int
        for(_s in _array_data) {
            _int_array_data.push( Std.parseInt( _s ) );
        }

        var texture_bytes = Lab.utils.arrayToBytes( _int_array_data );
        var texture = new Texture(resource_manager);

        #if lime_native 
            texture.create_from_bytes( _name , lime.utils.ByteArray.fromBytes(texture_bytes) );
        #end
        #if lime_html5
            texture.create_from_bytes_using_haxe(_name, texture_bytes );
        #end 

        texture_bytes = null;
        _array_data = null;
        _int_array_data = null;
        texture.loaded = true;

        resource_manager.cache(texture);

        return texture;        
<commit_msg>adding more debug<commit_after>package phoenix;

import lime.gl.GL;
import lime.utils.Libs;

import phoenix.ResourceManager;
import phoenix.geometry.Geometry;
    //default resources
import phoenix.defaults.Shaders;
import phoenix.defaults.FontString;
import phoenix.defaults.FontBytes;

import phoenix.Shader;
import phoenix.Color;
import phoenix.Camera;
import phoenix.Texture;
import phoenix.BitmapFont;

import lime.utils.UInt8Array;
import lime.utils.ArrayBuffer;

import phoenix.utils.BinarySearchTree;

class RendererStats {
    public function new(){}
    public var batchers : Int = 0;
    public var geometry_count : Int = 0;
    public var dynamic_batched_count : Int = 0;
    public var static_batched_count : Int = 0;
    public var enabled_count : Int = 0;
    public var draw_calls : Int = 0;
    public var group_count : Int = 0;
    public function toString() {
        return 
            'Renderer Statistics\n' + 
            '\tbatcher count : ' + batchers + '\n' +
            '\ttotal geometry : ' + geometry_count + '\n' +
            '\tenabled geometry : ' + enabled_count + '\n' +
            '\tdynamic batched geometry : ' + dynamic_batched_count + '\n' +
            '\tstatic batched geometry : ' + static_batched_count + '\n' +
            '\ttotal draw calls : ' + draw_calls;
    }
}

class Renderer {

    public var resource_manager : ResourceManager;
    public var batchers : BinarySearchTree<Batcher>;
        
        //Default rendering
    public var default_shader : Shader;
    public var default_shader_textured : Shader;
        //Default view and batching renderer
    public var default_batcher : Batcher;
    public var default_camera : Camera;
        //Default font for debug stuff etc
    public var default_font : BitmapFont;    

    public var should_clear : Bool = true;
    public var stop : Bool = false;
    public var stop_count : Int = 0;
    public var clear_color : Color;

    public var stats : RendererStats;   

    public function startup() {

        clear_color = new Color(0,0,0,1);
        stats = new RendererStats();
        

        resource_manager = new ResourceManager();
        batchers = new BinarySearchTree<Batcher>( function(a:Batcher,b:Batcher){
            return a.compare(b);
        } );

            //The default view
        default_camera = new Camera( ProjectionType.ortho, { x2 : Lab.screen.w, y2 : Lab.screen.h } );

            //create the default rendering shader
        default_shader = new Shader( resource_manager );  
        default_shader.id = 'default_shader';

        default_shader_textured = new Shader( resource_manager );  
        default_shader_textured.id = 'default_shader_textured';

        default_shader.load_from_string( Shaders.vertex_shader(), 
                                         Shaders.fragment_untextured(), false );

        default_shader_textured.load_from_string( Shaders.vertex_shader(), 
                                                  Shaders.fragment_textured(), false );

            //create the default batcher
        default_batcher = new Batcher( this, 'default_batcher' );
        add_batch(default_batcher);

            //create the default font
        default_font = new BitmapFont( resource_manager );

        trace("Creating the default font...");
            //create the font texture                    
        var _font_texture = load_texture_from_string_byte_array('default_font', FontBytes.data(), 512,512 );
        default_font.load_from_string( FontString.data(), 'phoenix.internal_data.default_font', null, [_font_texture] );

        trace("Done. " + _font_texture.width + 'x' + _font_texture.height );

            //enable z buffer use
        GL.enable(GL.DEPTH_TEST);
            // Accept fragment if it closer or equal away from the other
        GL.depthFunc(GL.LEQUAL);
            //Clear the depth buffer
        GL.clearDepth(1.0);

            //Enable blending
        GL.enable(GL.BLEND);
        GL.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);

            //Make sure that we aren't premultiplying the backbuffer
        #if lime_html5
            GL.pixelStorei(GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
        #end //html5
        
        // trace(':: phoenix :: renderer starting up');        
    }

    public function shutdown() {
        // trace(':: phoenix :: renderer shutting down');   
    }

    public function add_batch(batch:Batcher) {
        batchers.insert(batch);
    }

    public function clear( _color:Color ) {
        if(_color == null) _color = clear_color;

        GL.clearColor( _color.r, _color.g, _color.b, _color.a );
        GL.clear( GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT );
    }


    public function load_texture_from_string_byte_array(_name:String = 'untitled texture', _string_byte_array:String, _width:Int, _height:Int) : Texture {
        
        trace("Checking if the texture exists in the cache ...");
        var _exists = resource_manager.find_texture(_name);

        if(_exists != null) {
            trace("Yes, returning it instead");
            return _exists;
        } else {
            trace("Nope, continuing");
        }

        var _array_data = _string_byte_array.split(' ');
        var _int_array_data : Array<Int> = [];
            //loop over the string items and parse them to int
        for(_s in _array_data) {
            _int_array_data.push( Std.parseInt( _s ) );
        }

        trace("have data array");

        var texture_bytes = Lab.utils.arrayToBytes( _int_array_data );
        var texture = new Texture(resource_manager);

        #if lime_native 
            texture.create_from_bytes( _name , lime.utils.ByteArray.fromBytes(texture_bytes) );
            trace("created from bytes");
        #end
        #if lime_html5
            texture.create_from_bytes_using_haxe(_name, texture_bytes );
        #end 

        texture_bytes = null;
        _array_data = null;
        _int_array_data = null;
        texture.loaded = true;

        trace("add to cache");
        resource_manager.cache(texture);

        return texture;        

{'fraction_non_alphanumeric': 0.09451653411469234, 'fraction_numerical': 0.004520719966513186, 'mean_word_length': 2.3036504424778763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1958137', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3405, 'n_words': 842}