Skip to content

Commit

Permalink
Moved shader load to ClientProxy with SideOnly, and changed loading t…
Browse files Browse the repository at this point in the history
…o getResourceAsStream with rooted absolute path.
  • Loading branch information
dmon82 committed Sep 17, 2013
1 parent d17996b commit 0924d01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
46 changes: 0 additions & 46 deletions common/com/sirolf2009/necromancy/Necromancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Logger;

import org.lwjgl.opengl.ARBFragmentShader;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.ARBVertexShader;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.server.dedicated.PropertyManager;
Expand Down Expand Up @@ -79,19 +74,6 @@ public class Necromancy {
public void preInit(FMLPreInitializationEvent event) {
loggerNecromancy = Logger.getLogger("necromancy");
loggerNecromancy.setParent(FMLLog.getLogger());

try {
int vertShader = loadShader("assets/necromancy/shaders/scent/scentFragment.shader",GL20.GL_VERTEX_SHADER);
int fragShader = loadShader("assets/necromancy/shaders/scent/scentVertex.shader",GL20.GL_FRAGMENT_SHADER);
int scentProgram = GL20.glCreateProgram();
GL20.glAttachShader(scentProgram, vertShader);
GL20.glAttachShader(scentProgram, fragShader);
GL20.glLinkProgram(scentProgram);
GL20.glValidateProgram(scentProgram);
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}

ConfigurationNecromancy.initProperties(event);

Expand Down Expand Up @@ -156,34 +138,6 @@ private void init() {
GameRegistry.registerWorldGenerator(new WorldGenerator());
}

public int loadShader(String filename, int type) {
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;

try {
BufferedReader reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource(filename).toURI().getPath()));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Could not read file.");
e.printStackTrace();
System.exit(-1);
} catch (URISyntaxException e) {
System.err.println("Could not read file.");
e.printStackTrace();
System.exit(-1);
}

shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);

return shaderID;
}

private int createShader(String filename, int shaderType) throws Exception {
int shader = 0;
try {
Expand Down
45 changes: 45 additions & 0 deletions common/com/sirolf2009/necromancy/core/proxy/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.KeyBindingRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.VillagerRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import org.lwjgl.opengl.GL20;

public class ClientProxy extends CommonProxy {
static public Minecraft mc;
Expand Down Expand Up @@ -92,8 +99,46 @@ public void init() {

VillagerRegistry.instance().registerVillagerSkin(ConfigurationNecromancy.NecroVillagerID, ReferenceNecromancy.TEXTURES_ENTITIES_NECROMANCER);
VillagerRegistry.instance().registerVillageTradeHandler(ConfigurationNecromancy.NecroVillagerID, Necromancy.PacketHandler);

try {
int vertShader = loadShader("/assets/necromancy/shaders/scent/scentFragment.shader",GL20.GL_VERTEX_SHADER);
int fragShader = loadShader("/assets/necromancy/shaders/scent/scentVertex.shader",GL20.GL_FRAGMENT_SHADER);
int scentProgram = GL20.glCreateProgram();
GL20.glAttachShader(scentProgram, vertShader);
GL20.glAttachShader(scentProgram, fragShader);
GL20.glLinkProgram(scentProgram);
GL20.glValidateProgram(scentProgram);
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
}

@SideOnly(Side.CLIENT)
public int loadShader(String filename, int type) {
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(Necromancy.class.getResourceAsStream(filename)));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Could not read file.");
e.printStackTrace();
System.exit(-1);
}

shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);

return shaderID;
}

public static void spawnParticle(String name, double posX, double posY, double posZ, double motionX, double motionY, double motionZ) {
if (mc != null && mc.renderViewEntity != null && mc.effectRenderer != null) {
if (name.equals("skull")) {
Expand Down

0 comments on commit 0924d01

Please sign in to comment.