001package com.fs.starfarer.api.impl.campaign;
002
003import java.awt.Color;
004
005import org.lwjgl.opengl.GL11;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.SectorEntityToken;
009import com.fs.starfarer.api.graphics.SpriteAPI;
010import com.fs.starfarer.api.impl.campaign.ids.Tags;
011import com.fs.starfarer.api.util.Misc;
012
013public class GenericCampaignEntitySprite {
014
015        //public static final float CELL_SIZE = 32;
016        
017        protected SpriteAPI shadowMask;
018        protected SpriteAPI sprite;
019        protected SpriteAPI glow;
020        protected SpriteAPI overlay;
021        private float overlayAngleOffset = 0f;
022        
023        private float scale;
024
025        
026        protected SectorEntityToken entity;
027        
028        public GenericCampaignEntitySprite(SectorEntityToken entity, String spriteName, float scale) {
029                this.entity = entity;
030                this.scale = scale;
031                sprite = Global.getSettings().getSprite(spriteName);
032
033                float width = sprite.getWidth() * scale;
034                float height = sprite.getHeight() * scale;
035                sprite.setSize(width, height);
036                
037                shadowMask = Global.getSettings().getSprite("graphics/fx/ship_shadow_mask.png");
038                float max = Math.max(width, height);
039                shadowMask.setSize(max * 1.5f, max * 1.5f);
040        }
041        
042        public SpriteAPI getGlow() {
043                return glow;
044        }
045
046        public void setGlow(SpriteAPI glow) {
047                this.glow = glow;
048        }
049
050        public SpriteAPI getOverlay() {
051                return overlay;
052        }
053
054        public void setOverlay(SpriteAPI overlay) {
055                this.overlay = overlay;
056        }
057        
058        public float getOverlayAngleOffset() {
059                return overlayAngleOffset;
060        }
061
062        public void setOverlayAngleOffset(float overlayAngleOffset) {
063                this.overlayAngleOffset = overlayAngleOffset;
064        }
065
066        public void render(float cx, float cy, float facing, float alphaMult) {
067                if (alphaMult <= 0) return;
068                
069                GL11.glPushMatrix();
070                GL11.glTranslatef(entity.getLocation().x, entity.getLocation().y, 0);
071                
072                SectorEntityToken lightSource = entity.getLightSource();
073                
074                if (lightSource != null && entity.getLightColor() != null) {
075                        sprite.setColor(entity.getLightColor());
076                        if (overlay != null) {
077                                overlay.setColor(entity.getLightColor());
078                        }
079                } else {
080                        sprite.setColor(Color.white);
081                        if (overlay != null) {
082                                overlay.setColor(Color.white);
083                        }
084                }
085                                
086                sprite.setAngle(facing - 90f);
087                sprite.setNormalBlend();
088                sprite.setAlphaMult(alphaMult);
089                sprite.renderAtCenter(cx, cy);
090                                
091                if (glow != null) {
092                        glow.setAngle(facing - 90f);
093                        glow.setAdditiveBlend();
094                        glow.setAlphaMult(alphaMult);
095                        glow.renderAtCenter(cx, cy);
096                }
097                
098                if (overlay != null) {
099//                      overlay.setAngle(facing - 90f);
100//                      overlay.setNormalBlend();
101//                      overlay.setAlphaMult(alphaMult);
102//                      overlay.renderAtCenter(cx, cy);
103                        
104                        float w = overlay.getWidth() * 1.41f;
105                        float h = w;
106                        
107                        // clear out destination alpha in area we care about
108                        GL11.glColorMask(false, false, false, true);
109                        Misc.renderQuadAlpha(0 - w/2f - 1f, 0 - h/2f - 1f, w + 2f, h + 2f, Misc.zeroColor, 0f);
110                        
111                        sprite.setBlendFunc(GL11.GL_ONE, GL11.GL_ZERO);
112                        sprite.renderAtCenter(cx, cy);
113                        
114                        overlay.setAlphaMult(alphaMult);
115                        overlay.setAngle(facing - 90f + overlayAngleOffset);
116                        overlay.setBlendFunc(GL11.GL_ZERO, GL11.GL_SRC_ALPHA);
117                        overlay.renderAtCenter(cx, cy);
118                        
119                        GL11.glColorMask(true, true, true, false);
120                        overlay.setBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE_MINUS_DST_ALPHA);
121                        overlay.renderAtCenter(cx, cy);
122                }
123                
124                if (lightSource != null && !entity.getLightSource().hasTag(Tags.AMBIENT_LS)) {
125                        float w = shadowMask.getWidth() * 1.41f;
126                        float h = w;
127                        
128                        // clear out destination alpha in area we care about
129                        GL11.glColorMask(false, false, false, true);
130                        Misc.renderQuadAlpha(0 - w/2f - 1f, 0 - h/2f - 1f, w + 2f, h + 2f, Misc.zeroColor, 0f);
131                        
132                        sprite.setBlendFunc(GL11.GL_ONE, GL11.GL_ZERO);
133                        sprite.renderAtCenter(cx, cy);
134                        
135                        float lightDir = Misc.getAngleInDegreesStrict(entity.getLocation(), lightSource.getLocation());
136                        shadowMask.setAlphaMult(alphaMult);
137                        shadowMask.setAngle(lightDir);
138                        shadowMask.setBlendFunc(GL11.GL_ZERO, GL11.GL_SRC_ALPHA);
139                        shadowMask.renderAtCenter(cx, cy);
140                        
141                        GL11.glColorMask(true, true, true, false);
142                        shadowMask.setBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE_MINUS_DST_ALPHA);
143                        shadowMask.renderAtCenter(cx, cy);
144                }
145                
146                GL11.glPopMatrix();
147        }
148        
149        
150}
151
152
153
154
155
156
157
158
159
160
161
162
163
164