Archived
0

Init project

This commit is contained in:
2013-10-09 09:17:49 +04:00
commit c8ce51596e
12 changed files with 465 additions and 0 deletions

15
.gitignore vendored Normal file
View File

@@ -0,0 +1,15 @@
## Common ##
lib/
target/
*.rar
## IDEA ##
.idea/
out/
*.iml
## Eclipse ##
.settings/
bin/
.classpath
.project

10
build.properties Normal file
View File

@@ -0,0 +1,10 @@
source.dir = src
resource.dir = resource
lib.dir = lib
target.dir = target
classes.dir = ${target.dir}/classes
lwjgl.version = 2.9.0
jinput.version = 2.0.5
launch4j.version = 3.1.0-beta2

163
build.xml Normal file
View File

@@ -0,0 +1,163 @@
<?xml version="1.0"?>
<project name="The Game" default="build">
<property file="build.properties" />
<target name="build" depends="unpack-natives, jar">
<mkdir dir="${target.dir}/lib/native"/>
<copy todir="${target.dir}/lib">
<fileset dir="${lib.dir}">
<include name="jinput.jar"/>
<include name="lwjgl.jar"/>
<include name="lwjgl_util.jar"/>
</fileset>
</copy>
<copy todir="${target.dir}/lib/native">
<fileset dir="${lib.dir}/native"/>
</copy>
</target>
<target name="clear">
<delete dir="${target.dir}"/>
</target>
<target name="init" description="Инициализация сборки">
<mkdir dir="${lib.dir}/native"/>
<mkdir dir="${classes.dir}"/>
<mkdir dir="${target.dir}/exe"/>
<condition property="check-libs">
<and>
<available file="${lib.dir}/lwjgl.jar" type="file"/>
<available file="${lib.dir}/lwjgl_util.jar" type="file"/>
<available file="${lib.dir}/lwjgl-platform.jar" type="file"/>
<available file="${lib.dir}/jinput.jar" type="file"/>
<available file="${lib.dir}/jinput-platform.jar" type="file"/>
</and>
</condition>
<fileset dir="${lib.dir}/native" id="native.fileset"/>
<property name="native.files" refid="native.fileset"/>
<condition property="check-native">
<length string="${native.files}" length="0"/>
</condition>
<condition property="exe-check-lib">
<available file="${lib.dir}/launch4j.zip" type="file"/>
</condition>
<condition property="exe-check-tool">
<and>
<available file="${target.dir}/exe/launch4j/bin" type="dir"/>
<available file="${target.dir}/exe/launch4j/head" type="dir"/>
<available file="${target.dir}/exe/launch4j/lib" type="dir"/>
<available file="${target.dir}/exe/launch4j/manifest" type="dir"/>
<available file="${target.dir}/exe/launch4j/sign4j" type="dir"/>
<available file="${target.dir}/exe/launch4j/w32api" type="dir"/>
<available file="${target.dir}/exe/launch4j/launch4j.jar" type="file"/>
<available file="${target.dir}/exe/launch4j/launch4j.jfpr" type="file"/>
</and>
</condition>
</target>
<target name="download-libs" unless="check-libs" depends="init" description="Загрузка необходимых библиотек">
<get dest="${lib.dir}/lwjgl.jar" src="http://search.maven.org/remotecontent?filepath=org/lwjgl/lwjgl/lwjgl/${lwjgl.version}/lwjgl-${lwjgl.version}.jar" usetimestamp="true" />
<get dest="${lib.dir}/lwjgl_util.jar" src="http://search.maven.org/remotecontent?filepath=org/lwjgl/lwjgl/lwjgl_util/${lwjgl.version}/lwjgl_util-${lwjgl.version}.jar" usetimestamp="true" />
<get dest="${lib.dir}/lwjgl-platform.jar" src="http://search.maven.org/remotecontent?filepath=org/lwjgl/lwjgl/lwjgl-platform/${lwjgl.version}/lwjgl-platform-${lwjgl.version}-natives-windows.jar" usetimestamp="true" />
<get dest="${lib.dir}/jinput.jar" src="http://search.maven.org/remotecontent?filepath=net/java/jinput/jinput/${jinput.version}/jinput-${jinput.version}.jar" usetimestamp="true" />
<get dest="${lib.dir}/jinput-platform.jar" src="http://search.maven.org/remotecontent?filepath=net/java/jinput/jinput-platform/${jinput.version}/jinput-platform-${jinput.version}-natives-windows.jar" usetimestamp="true" />
</target>
<target name="unpack-natives" if="check-native" depends="download-libs" description="Распаковка библиотек операционной системы">
<unzip src="${lib.dir}/lwjgl-platform.jar" dest="${lib.dir}/native">
<patternset>
<exclude name="META-INF/"/>
</patternset>
</unzip>
<unzip src="${lib.dir}/jinput-platform.jar" dest="${lib.dir}/native">
<patternset>
<exclude name="META-INF/"/>
</patternset>
</unzip>
</target>
<target name="compile" depends="init" description="Компиляция">
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<javac
classpathref="classpath"
srcdir="${source.dir}"
destdir="${classes.dir}"
compiler="javac1.7"
source="1.7"
target="1.7"
debug="true"
debuglevel="lines,vars,source"
encoding="UTF-8"
optimize="true"
includeantruntime="false"/>
</target>
<target name="jar" depends="compile" description="Сборка в единый java архив">
<tstamp>
<format property="BUILD_TIME" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<jar destfile="${target.dir}/${ant.project.name}.jar">
<fileset dir="${classes.dir}"/>
<fileset dir="${resource.dir}/java"/>
<manifest>
<attribute name="Main-Class" value="ru.dmitriymx.game.Main"/>
<attribute name="Class-Path" value="./lib/lwjgl.jar ./lib/lwjgl_util.jar ./lib/jinput.jar"/>
<attribute name="Build-By" value="DmitriyMX"/>
<attribute name="Build-Date" value="${BUILD_TIME}"/>
</manifest>
</jar>
</target>
<target name="exe-download-lib" unless="exe-check-lib" depends="init">
<get dest="${lib.dir}/launch4j.zip" src="http://sourceforge.net/projects/launch4j/files/launch4j-3/${launch4j.version}/launch4j-${launch4j.version}-win32.zip/download" usetimestamp="true"/>
</target>
<target name="exe-unpack-tool" unless="exe-check-tool" depends="exe-download-lib">
<unzip src="${lib.dir}/launch4j.zip" dest="${target.dir}/exe">
<patternset>
<include name="launch4j/bin/"/>
<include name="launch4j/head/"/>
<include name="launch4j/lib/"/>
<include name="launch4j/manifest/"/>
<include name="launch4j/sign4j/"/>
<include name="launch4j/w32api/"/>
<include name="launch4j/launch4j.jar"/>
<include name="launch4j/launch4j.jfpr"/>
</patternset>
</unzip>
</target>
<target name="exe-build" depends="exe-unpack-tool">
<taskdef name="launch4j" classname="net.sf.launch4j.ant.Launch4jTask"
classpath="${target.dir}/exe/launch4j/launch4j.jar:${target.dir}/exe/launch4j/lib/xstream.jar"/>
<launch4j>
<config headerType="gui"
outfile="${target.dir}/${ant.project.name}.exe"
dontWrapJar="false"
jarPath="${target.dir}/${ant.project.name}.jar"
downloadUrl="http://www.oracle.com/technetwork/java/javase/downloads/index.html"
icon="${resource.dir}/exe/icon.ico">
<jre minVersion="1.7.0">
<opt>-Djava.library.path=lib/native</opt>
</jre>
</config>
</launch4j>
</target>
</project>

BIN
resource/exe/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

View File

@@ -0,0 +1,39 @@
package ru.dmitriymx.game;
import org.lwjgl.opengl.GL11;
public class Foxy {
private Sprite texture;
private int x, y;
public Foxy(){
texture = new Sprite(Foxy.class.getResourceAsStream("/ru/dmitriymx/game/foxy2.png"), 38, 33);
x = y = 0;
}
public void render(){
Sprite.Coords frame = texture.getFrame();
final int K = 2;
GL11.glColor3f(1f, 1f, 1f);
texture.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y1));
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y1));
GL11.glVertex2f(texture.getWidthSprite()*K, 0);
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y2));
GL11.glVertex2f(texture.getWidthSprite()*K, texture.getHeightSprite()*K);
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y2));
GL11.glVertex2f(0, texture.getHeightSprite()*K);
GL11.glEnd();
texture.nextFrame();
}
}

View File

@@ -0,0 +1,58 @@
package ru.dmitriymx.game;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Main {
private Foxy foxy;
private void init_display(int width, int height){
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setVSyncEnabled(true);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(-1);
}
}
private void init_opengl(int width, int height) {
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0, 0, 0.3f, 0);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
private void render(){
foxy.render();
}
public void start(int width, int height){
init_display(width, height);
init_opengl(width, height);
foxy = new Foxy();
while(!Display.isCloseRequested()){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
render();
Display.update();
Display.sync(12);
}
Display.destroy();
}
public static void main(String[] args){
new Main().start(800,600);
}
}

View File

@@ -0,0 +1,66 @@
package ru.dmitriymx.game;
import java.awt.image.BufferedImage;
import java.io.InputStream;
public class Sprite extends Texture {
public class Coords{
int x1,y1,x2,y2;
Coords(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
private Coords[] frame;
private int max_frames;
private int current_frame = 0;
private int width, height;
private void _init(){
double v1 = Math.floor(this.getWidth() / width);
double v2 = Math.floor(this.getHeight() / height);
max_frames = (int)(v1 * v2);
frame = new Coords[max_frames];
for(int i = 0; i < max_frames; i++){
frame[i] = new Coords(i*width, i*height, (i+1)*width, (i+1)*height);
}
}
public Sprite(BufferedImage image, int width, int height) {
super(image);
this.width = width;
this.height = height;
_init();
}
public Sprite(InputStream stream, int width, int height) {
super(stream);
this.width = width;
this.height = height;
_init();
}
public Coords getFrame(){
return frame[current_frame];
}
public void nextFrame(){
if(current_frame == (max_frames-1)){
current_frame = 0;
} else {
current_frame++;
}
System.out.println(current_frame);
}
public int getWidthSprite(){
return width;
}
public int getHeightSprite(){
return height;
}
}

View File

@@ -0,0 +1,114 @@
package ru.dmitriymx.game;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
public class Texture {
private BufferedImage image;
private int width, height;
private int bind_id;
private float one_pixel_w, one_pixel_h;
private IntBuffer imageData;
private int rgb_array_size;
private void _init(BufferedImage image){
bind_id = GL11.glGenTextures();
this.image = image;
prepare_opengl();
prepare_image();
}
public Texture(BufferedImage image) {
_init(image);
}
public Texture(InputStream stream) {
try {
BufferedImage image = ImageIO.read(stream);
stream.close();
_init(image);
} catch (IOException e) {
e.printStackTrace();
}
}
private void prepare_opengl(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
}
private void prepare_image(){
if(width != image.getWidth() || height != image.getHeight()){
width = image.getWidth();
height = image.getHeight();
one_pixel_w = (100F / width) / 100F;
one_pixel_h = (100F / height) / 100F;
rgb_array_size = width * height;
imageData = ByteBuffer.allocateDirect(4 * rgb_array_size).order(ByteOrder.nativeOrder()).asIntBuffer();
}else{
imageData.clear();
}
int[] rgb_array = new int[rgb_array_size];
image.getRGB(0, 0, width, height, rgb_array, 0, width);
imageData.put(rgb_array);
imageData.position(0).limit(rgb_array_size);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, imageData);
}
public void bind(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id);
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public BufferedImage getImage(){
return image;
}
public void setImage(BufferedImage image){
this.image = image;
prepare_image();
}
public void setImage(InputStream stream){
try{
BufferedImage image = ImageIO.read(stream);
stream.close();
setImage(image);
} catch(Exception e){
e.printStackTrace();
}
}
public void updateTexture(){
prepare_image();
}
public float floatX(int x){
return one_pixel_w * x;
}
public float floatY(int y){
return one_pixel_h * y;
}
}