<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AquilaX's development blog &#187; Slick</title>
	<atom:link href="http://dev.horemag.net/category/java/slick/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.horemag.net</link>
	<description>code and so on...</description>
	<lastBuildDate>Sun, 25 Apr 2010 07:48:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Smooth tile scrolling with Slick</title>
		<link>http://dev.horemag.net/2009/05/05/smooth-tile-scrolling-with-slick/</link>
		<comments>http://dev.horemag.net/2009/05/05/smooth-tile-scrolling-with-slick/#comments</comments>
		<pubDate>Tue, 05 May 2009 05:27:10 +0000</pubDate>
		<dc:creator>AquilaX</dc:creator>
				<category><![CDATA[Slick]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[tile-based]]></category>
		<category><![CDATA[tiles]]></category>

		<guid isPermaLink="false">http://dev.horemag.net/?p=89</guid>
		<description><![CDATA[
This is my first attempt for smooth tile scrolling in Slick. The offx/offy coordinates are not entirely correct for hero, smaller than the tile image but it's a good start the "smooth" part comes from shx/shy variables which offset the rendering position. The code is after the break:
Update: Check the project source code page at [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.horemag.net/wp-content/uploads/game1.png" alt="game1" title="game1" width="299" height="254" class="alignnone size-full wp-image-92" /></p>
<p>This is my first attempt for smooth tile scrolling in Slick. The offx/offy coordinates are not entirely correct for hero, smaller than the tile image but it's a good start the "smooth" part comes from shx/shy variables which offset the rendering position. The code is after the break:</p>
<p><strong>Update:</strong> Check the project source code page at Google Code: <a href="http://code.google.com/p/jmuonline/" onclick="javascript:urchinTracker('/outbound/article/http://code.google.com/p/jmuonline/');">http://code.google.com/p/jmuonline/</a></p>
<p><span id="more-89"></span></p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> scroller;
&nbsp;
<span style="color: #a1a100;">import org.newdawn.slick.AppGameContainer;</span>
<span style="color: #a1a100;">import org.newdawn.slick.BasicGame;</span>
<span style="color: #a1a100;">import org.newdawn.slick.Color;</span>
<span style="color: #a1a100;">import org.newdawn.slick.GameContainer;</span>
<span style="color: #a1a100;">import org.newdawn.slick.Graphics;</span>
<span style="color: #a1a100;">import org.newdawn.slick.Input;</span>
<span style="color: #a1a100;">import org.newdawn.slick.SlickException;</span>
<span style="color: #a1a100;">import org.newdawn.slick.tiled.TiledMap;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 *
 * @author aquilax
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Scroller2 <span style="color: #000000; font-weight: bold;">extends</span> BasicGame<span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> TiledMap map = <span style="color: #000000; font-weight: bold;">null</span>;
  <span style="color: #000000; font-weight: bold;">private</span> Hero hero = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> screenw = <span style="color: #cc66cc;">432</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> screenh = <span style="color: #cc66cc;">336</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> centerx = screenw/<span style="color: #cc66cc;">2</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> centery = screenh/<span style="color: #cc66cc;">2</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> visx = <span style="color: #cc66cc;">9</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> visy = <span style="color: #cc66cc;">7</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">float</span> halfvisx = visx/<span style="color: #cc66cc;">2</span>;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">float</span> halfvisy = visy/<span style="color: #cc66cc;">2</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> tileW;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> tileH;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> mapW;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> mapH;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> blocked;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> offx;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> offy;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Scroller2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Scroller2&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span>GameContainer container<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SlickException <span style="color: #66cc66;">&#123;</span>
    map = <span style="color: #000000; font-weight: bold;">new</span> TiledMap<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;data/testone.tmx&quot;</span><span style="color: #66cc66;">&#41;</span>;
    tileW = map.<span style="color: #006600;">getTileWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    tileH = map.<span style="color: #006600;">getTileHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    mapW = map.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    mapH = map.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    fillBlocked<span style="color: #66cc66;">&#40;</span>map<span style="color: #66cc66;">&#41;</span>;
    hero = <span style="color: #000000; font-weight: bold;">new</span> Hero<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;data/hero.png&quot;</span><span style="color: #66cc66;">&#41;</span>;
    hero.<span style="color: #006600;">speed</span> = <span style="color: #cc66cc;">1</span>;
    hero.<span style="color: #006600;">xtile</span> = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky" onclick="javascript:urchinTracker('/outbound/article/http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky');"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a>.<span style="color: #006600;">parseInt</span><span style="color: #66cc66;">&#40;</span>map.<span style="color: #006600;">getMapProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;herox&quot;</span>, <span style="color: #ff0000;">&quot;16&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    hero.<span style="color: #006600;">ytile</span> = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky" onclick="javascript:urchinTracker('/outbound/article/http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky');"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a>.<span style="color: #006600;">parseInt</span><span style="color: #66cc66;">&#40;</span>map.<span style="color: #006600;">getMapProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;heroy&quot;</span>, <span style="color: #ff0000;">&quot;8&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    hero.<span style="color: #006600;">xpos</span> = centerx - hero.<span style="color: #006600;">awidth</span>/<span style="color: #cc66cc;">2</span>;
    hero.<span style="color: #006600;">ypos</span> = centery - hero.<span style="color: #006600;">aheight</span>/<span style="color: #cc66cc;">2</span>;
&nbsp;
    offx = hero.<span style="color: #006600;">xtile*tileW</span>;
    offy = hero.<span style="color: #006600;">ytile*tileH</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> update<span style="color: #66cc66;">&#40;</span>GameContainer container, <span style="color: #993333;">int</span> delta<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SlickException <span style="color: #66cc66;">&#123;</span>
    Input input = container.<span style="color: #006600;">getInput</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>input.<span style="color: #006600;">isKeyDown</span><span style="color: #66cc66;">&#40;</span>Input.<span style="color: #006600;">KEY_LEFT</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      moveChar<span style="color: #66cc66;">&#40;</span>hero, <span style="color: #cc66cc;">-1</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>input.<span style="color: #006600;">isKeyDown</span><span style="color: #66cc66;">&#40;</span>Input.<span style="color: #006600;">KEY_RIGHT</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      moveChar<span style="color: #66cc66;">&#40;</span>hero, <span style="color: #cc66cc;">+1</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>input.<span style="color: #006600;">isKeyDown</span><span style="color: #66cc66;">&#40;</span>Input.<span style="color: #006600;">KEY_UP</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      moveChar<span style="color: #66cc66;">&#40;</span>hero, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>input.<span style="color: #006600;">isKeyDown</span><span style="color: #66cc66;">&#40;</span>Input.<span style="color: #006600;">KEY_DOWN</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      moveChar<span style="color: #66cc66;">&#40;</span>hero, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> render<span style="color: #66cc66;">&#40;</span>GameContainer container, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGraphics+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky" onclick="javascript:urchinTracker('/outbound/article/http://www.google.com/search?hl=en&amp;q=allinurl%3AGraphics+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky');"><span style="color: #aaaadd; font-weight: bold;">Graphics</span></a> g<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SlickException <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">int</span> shx = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>offx/tileW<span style="color: #66cc66;">&#41;</span>*tileW - offx;
    <span style="color: #993333;">int</span> shy = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>offy/tileH<span style="color: #66cc66;">&#41;</span>*tileH - offy;
    map.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span>shx, shy, <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>offx/tileW-halfvisx<span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>offy/tileH-halfvisy<span style="color: #66cc66;">&#41;</span>, visx<span style="color: #cc66cc;">+1</span>, visy<span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span>;
    hero.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>hero.<span style="color: #006600;">xpos</span>, hero.<span style="color: #006600;">ypos</span><span style="color: #66cc66;">&#41;</span>;
    g.<span style="color: #006600;">setColor</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky" onclick="javascript:urchinTracker('/outbound/article/http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky');"><span style="color: #aaaadd; font-weight: bold;">Color</span></a>.<span style="color: #006600;">red</span><span style="color: #66cc66;">&#41;</span>;
    g.<span style="color: #006600;">drawString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;offx &quot;</span>+offx, <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
    g.<span style="color: #006600;">drawString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;offy &quot;</span>+offy, <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky" onclick="javascript:urchinTracker('/outbound/article/http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;bntI=I%27m%20Feeling%20Lucky');"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SlickException <span style="color: #66cc66;">&#123;</span>
    AppGameContainer app = <span style="color: #000000; font-weight: bold;">new</span> AppGameContainer<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Scroller2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    app.<span style="color: #006600;">setDisplayMode</span><span style="color: #66cc66;">&#40;</span>screenw, screenh, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
    app.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> fillBlocked<span style="color: #66cc66;">&#40;</span>TiledMap map<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    blocked = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">boolean</span><span style="color: #66cc66;">&#91;</span>mapW<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>mapH<span style="color: #66cc66;">&#93;</span>;
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x = <span style="color: #cc66cc;">0</span>; x &lt; mapW; x++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> y = <span style="color: #cc66cc;">0</span>; y &lt; mapH; y++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        blocked<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span>  = <span style="color: #ff0000;">&quot;true&quot;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>map.<span style="color: #006600;">getTileProperty</span><span style="color: #66cc66;">&#40;</span>map.<span style="color: #006600;">getTileId</span><span style="color: #66cc66;">&#40;</span>x, y, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;blocked&quot;</span>, <span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> moveChar<span style="color: #66cc66;">&#40;</span>Hero ob, <span style="color: #993333;">int</span> dirx, <span style="color: #993333;">int</span> diry<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    offx += <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>dirx*ob.<span style="color: #006600;">speed</span><span style="color: #66cc66;">&#41;</span>;
    offy += <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>diry*ob.<span style="color: #006600;">speed</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://dev.horemag.net/2009/05/05/smooth-tile-scrolling-with-slick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
