public static SDL_Surface Resize(SDL_Surface surf,float factor)
 {
     SDL_Surface dest = new SDL_Surface();
     dest.name = surf.name;
     dest.w =(int) (surf.w*factor);
     dest.h =(int) (surf.h*factor);
     dest.Bitmap = new Texture2D (dest.w, dest.h, TextureFormat.RGBA32, false);
     float scale=(1/factor);
     for (int i=0; i<dest.w; i++) {
         for (int j=0; j<dest.h; j++) {
             int x = (int)(scale * i);
             int y = (int)(scale * j);
             dest.Bitmap.SetPixel (i, j, surf.Bitmap.GetPixel (x, y));
         }
     }
     dest.Bitmap.Apply ();
     dest.BitmapMaterial = new Material (Shader.Find ("Diffuse"));
     dest.BitmapMaterial.mainTexture = dest.Bitmap;
     return dest;
 }
 public static Anim anim_create(SDL_Surface surf)
 {
     return new Anim();
 }
 public static void putPixelBlack(SDL_Surface surf)
 {
     try{
         surf.Bitmap.SetPixel(0,0,Color.black);
         surf.Bitmap.SetPixel(surf.Bitmap.width,surf.Bitmap.height-1,Color.black);
         surf.Bitmap.SetPixel(surf.Bitmap.width,surf.Bitmap.height-2,Color.black);
         surf.Bitmap.SetPixel(surf.Bitmap.width-1,surf.Bitmap.height-1,Color.black);
         surf.Bitmap.SetPixel(surf.Bitmap.width-1,surf.Bitmap.height-2,Color.black);
         surf.Bitmap.SetPixel(0,surf.Bitmap.height-1,Color.black);
         surf.Bitmap.Apply();
     }
     catch(Exception e){
         Debug.LogError(e.Message);
     }
 }
 public static Color GetPixel(SDL_Surface surf, int x, int y)
 {
     return surf.bitmap.GetPixel(x, y);
 }
        /*
        load a surface from file putting it in soft or hardware mem
        */
        public static SDL_Surface LoadSurface(string fname, bool applyTransparency)
        {
            SDL_Surface sdl = new SDL_Surface();
            try
            {

                if (fname != null)
                {
                    sdl.bitmap = Resources.Load(fname) as Texture2D;
                    sdl.w = sdl.bitmap.width;
                    sdl.h = sdl.bitmap.height;
                    sdl.name = fname;
                    sdl.w = sdl.bitmap.width;
                    sdl.h = sdl.bitmap.height;
            #if TODO_RR
                    if (applyTransparency){
                        Color[] c = sdl.bitmap.GetPixels();
                        for (int i=0; i<c.Length; i++){
                            if (c[i]==Color.black){
                                c[i].b = c[i].b;
                                c[i].r = c[i].r;
                                c[i].g = c[i].g;
                                c[i].a = 0;

                            }
                            else{
                                c[i].b = c[i].b;
                                c[i].r = c[i].r;
                                c[i].g = c[i].g;
                                c[i].a = 1;
                            }
                        }
                        sdl.bitmap.SetPixels(c);
                        sdl.bitmap.Apply();
                        sdl.bitmapMaterial = new Material(Shader.Find("Transparent/Cutout/Diffuse"));
                        sdl.bitmapMaterial.mainTexture = sdl.bitmap;
                    }
                    else{
                        sdl.bitmapMaterial = new Material(Shader.Find("Diffuse"));
                        sdl.bitmapMaterial.mainTexture = sdl.bitmap;
                    }
            #endif
                    sdl.bitmapMaterial = new Material(Shader.Find("Diffuse"));
                    sdl.bitmapMaterial.mainTexture = sdl.bitmap;

                }
                return sdl;
            }
            catch (Exception e)
            {
                Debug.LogError("error trying to load and create a Texture2D " + fname);
                Debug.LogError(e.Message);
                return sdl;
                //throw e;
            }
        }
 public static Int32 SetPixel(SDL_Surface surf, int x, int y, int pixel)
 {
     surf.bitmap.SetPixel(x, y, Color.FromArgb(pixel));
     return surf.bitmap.GetPixel(x, y).ToArgb();
 }
 static void SDL_FreeSurface(SDL_Surface s)
 {
     throw new System.NotImplementedException ();
 }
        /// <summary>
        /// Draw tile units. If mask::fog is set no units are drawn.
        /// If 'ground' is True the ground unit is drawn as primary
        /// and the air unit is drawn small (and vice versa).
        /// If 'select' is set a selection frame is added.
        /// </summary>
        /// <param name="surf"></param>
        /// <param name="map_x"></param>
        /// <param name="map_y"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="ground"></param>
        /// <param name="select"></param>
        public SDL_Surface map_draw_units(SDL_Surface hexTex, int map_x, int map_y,bool ground, bool selected)
        {
            Player cur_player = Engine.cur_player;
            Unit unit = null;
            Map_Tile tile;

            if (map_x < 0 || map_y < 0 || map_x >= map_w || map_y >= map_h)
                throw new Exception ("Position out of map");
            tile = map [map_x, map_y];
            /* units */
            if (MAP_CHECK_VIS (map_x, map_y)) {
                if (tile.g_unit != null) {
                    unit = tile.g_unit;
                    bool resize = (ground || tile.a_unit == null)?false:true;
                    draw_unit_on_texture(hexTex,unit,resize);
                }
                if (tile.a_unit != null) {
                    unit = tile.a_unit;
                    bool resize = (!ground || tile.g_unit == null)?false:true;
                    draw_unit_on_texture(hexTex,unit,resize);
                }
                /* unit info icons */
                if (unit != null && Config.show_bar && unit.str>0) {
                    /* strength */
                    if ((cur_player != null) && Player.player_is_ally (cur_player, unit.player)){
                            string name = Unit.DeleteOrdinal (unit.name);
                            SDL_Surface sdl_str = SDL_Surface.LoadSurface(DB.UnitLib.unit_info_icons.str_img_name,false);
                            int offset = DB.UnitLib.unit_info_icons.str_h*(unit.str+15);
                            offset = sdl_str.h-offset;
                            SDL_Surface str = new SDL_Surface();
                            SDL_Surface.copy_image(str,DB.UnitLib.unit_info_icons.str_w,
                                                DB.UnitLib.unit_info_icons.str_h,sdl_str,0,offset);
                            int xdest = (Config.hex_w-DB.UnitLib.unit_info_icons.str_w)/2;
                            SDL_Surface.copy_image(hexTex,str,xdest,3,DB.UnitLib.unit_info_icons.str_w,
                                               DB.UnitLib.unit_info_icons.str_h,0,0);
                    }
                    else{
                        string name = Unit.DeleteOrdinal (unit.name);
                        SDL_Surface sdl_str = SDL_Surface.LoadSurface(DB.UnitLib.unit_info_icons.str_img_name,false);
                        int offset = DB.UnitLib.unit_info_icons.str_h*(unit.str);
                        offset = sdl_str.h-offset;
                        SDL_Surface str = new SDL_Surface();
                        SDL_Surface.copy_image(str,DB.UnitLib.unit_info_icons.str_w,
                                                DB.UnitLib.unit_info_icons.str_h,sdl_str,0,offset);
                        int xdest = (Config.hex_w-DB.UnitLib.unit_info_icons.str_w)/2;
                        SDL_Surface.copy_image(hexTex,str,xdest,3,DB.UnitLib.unit_info_icons.str_w,
                                               DB.UnitLib.unit_info_icons.str_h,0,0);

                    }
                    /* for current player only */
                    if (unit.player == cur_player) {
                        string name = Unit.DeleteOrdinal(unit.name);
                        Unit_Lib_Entry entry = DB.UnitLib.unit_lib_find_by_name(name);
                        /* attack */
                        if (unit.cur_atk_count>0){ //TODO_RR if (unit.cur_atk_count > 0) {
                            SDL_Surface atk = SDL_Surface.LoadSurface(DB.UnitLib.unit_info_icons.atk_img_name,false);
                            SDL_Surface.copy_image_without_key(hexTex,atk,15,3,Color.black);
                        }
                        /* move */
                        if (unit.cur_mov>0){ //TODO_RR if (unit.cur_mov > 0) {
                            SDL_Surface mov = SDL_Surface.LoadSurface(DB.UnitLib.unit_info_icons.mov_img_name,false);
                            SDL_Surface.copy_image_without_key(hexTex,mov,37,3,Color.black);
                        }
            #if TODO_RR
                        /* guarding */
                        if (unit.is_guarding) {
                            SDL_Surface.copy_image (surf.surf,
                             x + ((hex_w - DB.UnitLib.unit_info_icons.guard.w) >> 1),
                             y + hex_h - DB.UnitLib.unit_info_icons.guard.h,
                             DB.UnitLib.unit_info_icons.guard.w, DB.UnitLib.unit_info_icons.guard.h,
                             DB.UnitLib.unit_info_icons.guard, 0, 0);

                        }
            #endif
                    }
                }

            }
            /* selection frame */
            if (selected) {
                hexTex.BitmapMaterial.color = Color.green;
            }
            return hexTex;
        }
        public static void copy_image(SDL_Surface dest, SDL_Surface src,int xdest, 
			int ydest,int wsrc, int hsrc, int xsrc, int ysrc)
        {
            try{
                if (src==null || dest==null)
                    throw new Exception("the source or destination image is null");
                Color[] c = src.Bitmap.GetPixels(xsrc,ysrc,wsrc,hsrc);
                dest.Bitmap.SetPixels(xdest,ydest,wsrc, hsrc,c);
                dest.Bitmap.Apply();
                if (dest.BitmapMaterial!=null){
                    dest.BitmapMaterial.mainTexture = dest.Bitmap;
                }
                else{
                    dest.BitmapMaterial = new Material(Shader.Find("Diffuse"));
                    dest.BitmapMaterial.mainTexture = dest.Bitmap;
                }
            }
            catch(Exception e){
                Debug.LogError("Problems with the copy: "+ e.Message);
            }
        }
Exemple #10
0
 /// <summary>
 /// Draw terrain and units.
 /// </summary>
 /// <param name="surf"></param>
 /// <param name="map_x"></param>
 /// <param name="map_y"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="ground"></param>
 /// <param name="select"></param>
 public void map_draw_tile(SDL_Surface surf, int map_x, int map_y, int x, int y, bool ground, bool select)
 {
     map_draw_terrain (surf, map_x, map_y, x, y);
     map_draw_units (surf, map_x, map_y, x, y, ground, select);
 }
Exemple #11
0
        /// <summary>
        /// Draw a map tile terrain to surface. (fogged if mask::fog is set)
        /// </summary>
        /// <param name="surf"></param>
        /// <param name="map_x"></param>
        /// <param name="map_y"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public SDL_Surface map_draw_terrain(int map_x, int map_y)
        {
            int offset;
            string path;
            Map_Tile tile;
            if (map_x < 0 || map_y < 0 || map_x >= map_w || map_y >= map_h)
                throw new Exception ("Position of the tile out of the map");
            tile = map [map_x, map_y];
            if (tile.terrain.name.ToLower () == "mountain") {
                int numT = TextureTable.elegirImgTex (tile.strat_image_offset);
                path = Config.PathTexTerrain + tile.terrain.name.ToLower () + numT;
                offset = 0;
                if (numT == 1) {
                    offset = tile.strat_image_offset * Config.hex_w - Config.hex_w;
                } else {
                    offset = (tile.strat_image_offset - 39) * Config.hex_w - Config.hex_w;
                }

            } else {
                path = Config.PathTexTerrain + tile.terrain.name.ToLower ();
                offset = (tile.strat_image_offset * Config.hex_w) - Config.hex_w;
            }
            SDL_Surface terraintex = SDL_Surface.LoadSurface (path, false);
            SDL_Surface hextex = new SDL_Surface ();
            SDL_Surface.copy_image (hextex, Config.hex_w, Config.hex_h, terraintex, offset, 0);
            //Add texture flag
            if (tile.nation != null) {
                hextex = Nation.nation_draw_flag (tile.nation, hextex);
            }
            /* terrain */
            if (mask [map_x, map_y].fog) {
                hextex.BitmapMaterial.color = new Color(0.7f,0.7f,0.7f,1);
            }

            return hextex;
            #if TODO_RR
            /* grid */
            if (Config.grid) {
                SDL_Surface.copy_image (surf, x, y, hex_w, hex_h, Engine.terrain.terrainIcons.grid, 0, 0);
            }
            #endif
        }
Exemple #12
0
 /// <summary>
 /// Draw danger tile. Expects 'surf' to contain a fully drawn tile at
 /// the given position which will be tinted by overlaying the danger
 /// terrain surface.
 /// </summary>
 /// <param name="surf"></param>
 /// <param name="map_x"></param>
 /// <param name="map_y"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public void map_apply_danger_to_tile(SDL_Surface surf, int map_x, int map_y, int x, int y)
 {
     SDL_Surface.copy_image (surf, x, y, Engine.terrain.hex_w, Engine.terrain.hex_h,
                            Engine.terrain.terrainIcons.danger, 0, 0, (int)FOG_ALPHA.DANGER_ALPHA);
 }
Exemple #13
0
 private void draw_unit_on_texture(SDL_Surface hexTex, Unit unit, bool resize)
 {
     int ntex = TextureTable.elegirImgUnit (unit.sel_prop);
     SDL_Surface sdl = SDL_Surface.LoadSurface (unit.sel_prop.icon_img_name + 1, false);
     SDL_Surface dest = new SDL_Surface ();
     if (ntex == 1) {
         int offset = (sdl.h - unit.sel_prop.offset_img - unit.sel_prop.icon_h);
         SDL_Surface.copy_image (dest, unit.sel_prop.icon_w, unit.sel_prop.icon_h, sdl, 0, offset);
         SDL_Surface.putPixelBlack (dest);
         if (unit.orient == UnitLookingDirection.UNIT_ORIENT_LEFT) {
             SDL_Surface aux = new SDL_Surface ();
             SDL_Surface.copy_image180 (aux, dest);
             dest = aux;
         }
         if (dest.w > 51) {
             SDL_Surface aux = new SDL_Surface ();
             aux.Bitmap = new Texture2D (unit.sel_prop.icon_tiny_w, unit.sel_prop.icon_tiny_h, TextureFormat.RGB24, false);
             float scale = 1.5f;
             for (int i=0; i<unit.sel_prop.icon_tiny_w; i++) {
                 for (int j=0; j<unit.sel_prop.icon_tiny_h; j++) {
                     int x = (int)(scale * i);
                     int y = (int)(scale * j);
                     aux.Bitmap.SetPixel (i, j, dest.Bitmap.GetPixel (x, y));
                 }
             }
             aux.Bitmap.Apply ();
             aux.w = unit.sel_prop.icon_tiny_w;
             aux.h = unit.sel_prop.icon_tiny_h;
             aux.BitmapMaterial = new Material (Shader.Find ("Diffuse"));
             aux.BitmapMaterial.mainTexture = aux.Bitmap;
             dest = aux;
         }
         int xdest = (Config.hex_w - dest.w) / 2;
         SDL_Surface.copy_image_without_key (hexTex, dest, xdest + 2, Nation.nation_flag_height + 5, Color.black);
     }
     else if (ntex == 2) {
         int offset = unit.sel_prop.offset_img + unit.sel_prop.icon_h;
         offset = offset - sdl.h;
         SDL_Surface sdl2 = SDL_Surface.LoadSurface (unit.sel_prop.icon_img_name + 2, false);
         offset = sdl2.h - offset;
         SDL_Surface.copy_image (dest, unit.sel_prop.icon_w, unit.sel_prop.icon_h, sdl2, 0, offset);
         SDL_Surface.putPixelBlack (dest);
         if (unit.orient == UnitLookingDirection.UNIT_ORIENT_LEFT) {
             SDL_Surface aux = new SDL_Surface ();
             SDL_Surface.copy_image180 (aux, dest);
             dest = aux;
         }
         if (resize) {
             SDL_Surface aux = new SDL_Surface ();
             aux.Bitmap = new Texture2D (unit.sel_prop.icon_tiny_w, unit.sel_prop.icon_tiny_h, TextureFormat.RGB24, false);
             float scale = 1.5f;
             for (int i=0; i<unit.sel_prop.icon_tiny_w; i++) {
                 for (int j=0; j<unit.sel_prop.icon_tiny_h; j++) {
                     int x = (int)(scale * i);
                     int y = (int)(scale * j);
                     aux.Bitmap.SetPixel (i, j, dest.Bitmap.GetPixel (x, y));
                 }
             }
             aux.Bitmap.Apply ();
             aux.w = unit.sel_prop.icon_tiny_w;
             aux.h = unit.sel_prop.icon_tiny_h;
             aux.BitmapMaterial = new Material (Shader.Find ("Diffuse"));
             aux.BitmapMaterial.mainTexture = aux.Bitmap;
             dest = aux;
         }
         int xdest = (Config.hex_w - dest.w) / 2;
         int ydest = (resize)? Nation.nation_flag_height + 1: Nation.nation_flag_height + 5;
         SDL_Surface.copy_image_without_key (hexTex, dest, xdest + 2, ydest, Color.black);
     }
 }
        /*
        ====================================================================
        Locals
        ====================================================================
        */
        /*
        ====================================================================
        Get the geometry of an icon in surface 'icons' by using the three
        measure dots in the left upper, right upper, left lower corner.
        ====================================================================
        */
        static void unit_get_icon_geometry(int icon_id, SDL_Surface icons, out int width, out int height, out int offset, out Int32 key)
        {
            Int32 mark;
            int y;
            int count = icon_id * 2; /* there are two pixels for one icon */

            /* nada white dot! take the first pixel found in the upper left corner as mark */
            mark = SDL_Surface.GetPixel(icons, 0, 0);
            /* compute offset */
            for (y = 0; y < icons.h; y++)
                if (SDL_Surface.GetPixel(icons, 0, y) == mark)
                {
                    if (count == 0) break;
                    count--;
                }
            offset = y;
            /* compute height */
            y++;
            while (y < icons.h && SDL_Surface.GetPixel(icons, 0, y) != mark)
                y++;
            height = y - offset + 1;
            /* compute width */
            y = offset;
            width = 1;
            while (SDL_Surface.GetPixel(icons, width, y) != mark)
                width++;
            width++;
            /* pixel beside left upper measure key is color key */
            key = SDL_Surface.GetPixel(icons, 1, offset);
        }
Exemple #15
0
        public static void SDL_SetColorKey(SDL_Surface dest, Color color_key)
        {
            try{
                Color[] c = dest.bitmap.GetPixels();
                for (int i=0; i<c.Length; i++){
                    if (c[i]==color_key){
                        c[i].b = c[i].b;
                        c[i].r = c[i].r;
                        c[i].g = c[i].g;
                        c[i].a = 0;

                    }
                    else{
                        c[i].b = c[i].b;
                        c[i].r = c[i].r;
                        c[i].g = c[i].g;
                        c[i].a = 1;
                    }
                }
                dest.Bitmap.SetPixels(c);
                dest.Bitmap.Apply();
                dest.BitmapMaterial=new Material(Shader.Find("Transparent/Cutout/Diffuse"));
                dest.BitmapMaterial.mainTexture = dest.Bitmap;
            }
            catch(Exception e){
                Debug.LogError(e.Message);
            }
        }
Exemple #16
0
        public static void copy_image_without_key(SDL_Surface dest,SDL_Surface src,int xdest, 
												  int ydest,Color key)
        {
            try{
                for (int i=0; i<src.w;i++){
                    for (int j=0; j<src.h;j++){
                        Color c = src.Bitmap.GetPixel(i,j);
                        if (c!=key){
                            dest.Bitmap.SetPixel(xdest+i,ydest+j,c);
                        }
                    }
                }
                dest.Bitmap.Apply();
                if (dest.BitmapMaterial!=null){
                    dest.BitmapMaterial.mainTexture = dest.Bitmap;
                }
                else{
                    dest.BitmapMaterial = new Material(Shader.Find("Diffuse"));
                    dest.BitmapMaterial.mainTexture = dest.Bitmap;
                }
            }
            catch(Exception e){
                Debug.LogError(e.Message);
            }
        }
Exemple #17
0
        public static SDL_Surface CreateSurface(int w, int h, bool f)
        {
            SDL_Surface sur = new SDL_Surface();
            /*
            SDL_PixelFormat* spf = SDL_GetVideoSurface()->format;
            if ((sur = SDL_CreateRGBSurface(f, w, h, spf->BitsPerPixel, spf->Rmask, spf->Gmask, spf->Bmask, spf->Amask)) == 0)
            {
                fprintf(stderr, "ERR: ssur_create: not enough memory to create surface...\n");
                exit(1);
            }
            */
            /*    if (f & SDL_HWSURFACE && !(sur->flags & SDL_HWSURFACE))
                    fprintf(stderr, "unable to create surface (%ix%ix%i) in hardware memory...\n", w, h, spf->BitsPerPixel);*/
            //            SDL_SetColorKey(sur, SDL_SRCCOLORKEY, 0x0);
            //           SDL_SetAlpha(sur, 0, 0); /* no alpha */

            sur.Bitmap = new System.Drawing.Bitmap(w, h);
            sur.name ="Creado vacio";

            return sur;
        }
Exemple #18
0
        public static void full_copy_image(SDL_Surface dest, SDL_Surface src)
        {
            if (src==null || dest==null)
                throw new Exception("the source or destination image is null");
            try{
                Texture2D nueva = new Texture2D(src.w,src.h,TextureFormat.RGB24,false);
                for (int i=0;i<src.w;i++){
                    for (int j=0; j<src.h;j++){
                        Color c = src.bitmap.GetPixel(i,j);
                        nueva.SetPixel(i,j,c);
                    }
                }
                nueva.Apply();
                dest = new SDL_Surface();
                dest.Bitmap = nueva;
                dest.BitmapMaterial = new Material(Shader.Find("Diffuse"));
                dest.BitmapMaterial.mainTexture = nueva;
                dest.w = src.w;
                dest.h = src.h;

            }
            catch(Exception e){
                Debug.LogError("Problems with the copy of image: " + e.Message);
            }
        }
Exemple #19
0
 public static void copy_image(SDL_Surface dest, int w, int h, SDL_Surface src, int xsrc, int ysrc)
 {
     if (src==null || dest==null)
         throw new Exception("the source or destination image is null");
     try{
         Color[] pixels = src.bitmap.GetPixels(xsrc,ysrc,w,h);
         Texture2D tex = new Texture2D (w, h,TextureFormat.RGB24,false);
         tex.SetPixels (pixels);
         tex.Apply ();
         dest.Bitmap = tex;
         if (dest.BitmapMaterial!=null){
             dest.BitmapMaterial.mainTexture = tex;
         }
         else{
             dest.BitmapMaterial = new Material(Shader.Find("Diffuse"));
             dest.BitmapMaterial.mainTexture = tex;
         }
         dest.w = w;
         dest.h = h;
     }
     catch(Exception e){
         Debug.LogError("Problems with the copy of image: " + e.Message);
     }
 }
 public static SDL_Surface nation_draw_flag(Nation nation, SDL_Surface surf)
 {
     if (surf==null){
         throw new Exception("the Surface is null");
     }
     SDL_Surface.copy_image(surf,Nation.nation_flags,20,3,Nation.nation_flag_width,
                             Nation.nation_flag_height,0,(Nation.nation_flags.h-nation.flag_offset-Nation.nation_flag_height));
     return surf;
 }