Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (731)
Games in Android Showcase (217)
games submitted by our members
Games in WIP (798)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 ... 4 5 [6] 7 8 ... 10
 51 
 on: 2017-06-18 04:16:14 
Started by theagentd - Last post by Archive
Wouldn't a circular array with a head and tail pointer suffice?

The tail pointer would point at the index at which the least used object (last added) object is.
The head pointer would point at the next available spot (or a taken spot if it has looped all the way around)

When you remove an element, simply set it to null in the array (make sure all operations have a null check)
To move an element from index N to the front, just save it to a temp var, set index N to null, then put the saved element at the head pointer and increment the head pointer.

Instead of the Element interface containing next and previous pointers, it could contain an index into the array so that lookups are O(1)

 52 
 on: 2017-06-18 01:43:53 
Started by theagentd - Last post by theagentd
Hey, everyone.

Today I found myself in a need of a least-recently-used (LRU) cache system. Essentially, I wanted a cache of the last 10 000 used objects. When a new object was to be loaded, I wanted to unload the oldest element in the cache. Before, the cache was only 96 elements, at which point looping through the list to find the least recently used element was perfectly fine for the load I had, but with 10 000 objects, that situation changed. I found that LinkedHashMap could be used as a makeshift cache, as it can be set to update the internal order of elements when they are accessed, but it just seemed inefficient and weird for what I actually wanted to accomplish here. Some research made me realize that a simple linked list was a good fit for this, as a double linked list can easily be maintained in LRU order. Basically, whenever an element in the cache is used, it is moved to the beginning of the list (an O(1) operation on linked list), meaning that the cache stores objects sorted from most recently used to least recently used. Then to remove the most least recently used object, I just remove the tail of the list, again an O(1) operation.

Now, I hope we all know how bad Java's LinkedList class is. Not only does it generate garbage every time an object is added to it, but moving an object to the beginning of a list is an O(n) operation as I'd need to use list.remove(object), which would need to scan through the list until it finds the object in question and can remove it. A proper linked list implementation would store the previous and next references in the object itself, meaning that finding the previous and next objects become O(1) operations, and the element can be removed without going through the entire list. So... I wrote a minimal linked list that could do what I needed and not much more.

As mentioned, the main difference is that my linked list class does not allocate Node objects to store the next and previous references, but instead stores those references in the elements themselves. This leads to a couple of peculiarities.

 - All elements must implement an interface that allows the list to set the previous and next pointers (or alternatively extend a class which implements those functions for it).
 - You can't place the same element in a linked list twice, as each element can only track one previous and next neighbor.
 - For the same reason, you can't even use the same element in two DIFFERENT lists at the same time.

In my case, none of these were an issue, so I went ahead and implemented it. I even added a special method for moving an element in the list to the start of the list for maximum performance in that sense. The class provides the following functions, all of which are O(1):

 - addFirst(e)
 - addLast(e)
 - moveToFirst(e)
 - remove(e)
 - removeFirst()
 - removeLast()

There is no random access function. The correct way of looping through the list is to simply call element.getNext() until it returns null (the current size of the list is tracked though). The somewhat complicated usage of generics is there to allow for type safety, both when extending the Element class and when working with FastLinkedList.

Usage example:
1  
2  
3  
4  
5  
6  
7  
8  
private class MyElement extends FastLinkedList.SimpleElement<MyElement>{ //Note this definition here, it's particularly clever =P
    ...
}


FastLinkedList<MyElement> list = new FastLinkedList<>();
list.addFirst(...);
...



Performance test on 100 000 randomly shuffled elements:

Adding 100 000 objects to the list:
    LinkedList: 1.617 ms
    FastLinkedList: 0.627 ms
~3x faster

Move 2000 random elements from their current position to the start of the list:
    LinkedList: 203.315 ms
    FastLinkedList: 0.118 ms
Insanely much faster (O(n)--->O(1))

Remove 2000 random elements from the list:
    LinkedList: 175.541 ms
    FastLinkedList: 0.094 ms
Insanely much faster (O(n)--->O(1))

Remove remaining 98 000 objects using removeFirst():
    LinkedList: 0.298 ms
    FastLinkedList: 2.78 ms
Noticeably slower in this test due to worse cache coherency. FastLinkedList needs to access each element's data to find the next and previous node in the list. As the elements are shuffled, this ends up jumping around randomly in memory. Java's LinkedList instead creates Node objects for each element, which end up sequentially in memory as they're recreated each iteration. This is not an issue with real-life data, and is the cost of going garbage-less.




Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
package engine.util;

import engine.util.FastLinkedList.Element; //this import is required

public class FastLinkedList<E extends Element<E>> {
   
   private E head, tail;
   private int size;
   
   
   public FastLinkedList() {}

   
   public void addFirst(E element){
      if(size == 0){
         head = element;
         tail = element;
      }else{
         head.setPrevious(element);
         element.setNext(head);
         head = element;
      }
      size++;
   }
   
   public void addLast(E element){
      if(size == 0){
         head = element;
         tail = element;
      }else{
         tail.setNext(element);
         element.setPrevious(tail);
         tail = element;
      }
      size++;
   }
   
   public void moveToFirst(E element){
     
      if(element == head){
         return;
      }
     
      E prev = element.getPrevious();
      E next = element.getNext();
     
      prev.setNext(next); //prev cannot be null thanks to if-statement above
      if(next != null){
         next.setPrevious(prev);
      }else{
         //element was tail, update the tail.
         tail = prev;
      }
     
      element.setPrevious(null);
      element.setNext(head);
      head.setPrevious(element);
      head = element;
   }
   
   public void remove(E element){
      E prev = element.getPrevious();
      E next = element.getNext();
     
      if(prev != null){
         prev.setNext(next);
      }else{
         //prev == null means element was head.
         head = next;
      }
     
      if(next != null){
         next.setPrevious(prev);
      }else{
         //next == null means element was tail.
         tail = prev;
      }
     
      element.setPrevious(null);
      element.setNext(null);
     
      size--;
   }
   
   public E removeFirst(){
     
      E h = head;
     
     
      E next = h.getNext();
      if(next != null){
         next.setPrevious(null);
      }else{
         //next and prev are null, list is now empty
         tail = null;
      }

      h.setNext(null);
     
      head = next;

      size--;
     
      return h;
   }
   
   public E removeLast(){
     
      E t = tail;
     
      E prev = t.getPrevious();
      if(prev != null){
         prev.setNext(null);
      }else{
         //next and prev are null, list is now empty
         head = null;
      }
      t.setPrevious(null);
     
      tail = prev;
     
      size--;
     
      return t;
   }
   
   public E getFirst(){
      return head;
   }
   
   public E getLast(){
      return tail;
   }
   
   public int size() {
      return size;
   }
   
   public boolean isEmpty(){
      return size == 0;
   }
   
   public String toString() {
      StringBuilder b = new StringBuilder("[");
      E e = head;
      for(int i = 0; i < size; i++){
         b.append(e.toString());
         if(i != size-1){
            b.append(", ");
         }
         e = e.getNext();
      }
      return b.append(']').toString();
   }
   
   public static interface Element<E extends Element>{
     
      public void setNext(E next);
      public void setPrevious(E previous);
     
      public E getNext();
      public E getPrevious();
   }

   public static class SimpleElement<E extends SimpleElement> implements Element<E>{
     
      private E next, previous;

      @Override
      public void setNext(E next) {
         this.next = next;
      }

      @Override
      public void setPrevious(E previous) {
         this.previous = previous;
      }

      @Override
      public E getNext() {
         return next;
      }

      @Override
      public E getPrevious() {
         return previous;
      }
   }
}

 53 
 on: 2017-06-18 01:42:29 
Started by Jotunn-nooks - Last post by philfrei
Sounds to me like you are itching to learn more. Some people have a game idea that they want to bring to life, and not so worried about gaining programming skills. It makes sense that they would not wish to "waste" a lot of time learning coding to any great depth.

I've been amazed at what can be done with the "Blueprints" stuff that comes with Unreal. The tool set is incredible. I assume the same is true for Unity. I could see making all sorts of games within the bounds of what they have made available.

Completing a game and marketing it is a considerable accomplishment. But it is going to help more with design work and showing you can see something through to the end than it will show programming skills.

I really don't know what to advise, beyond checking out Gamasutra, perhaps, for the job listings that are current, and deciding if some of them are worth targeting when you decide what to study or do next.

I decided to go my own way with audio coding, and have learned a lot, about both audio and Java. I had some notions that by getting off the beaten path and building something myself, from scratch, I would learn interesting things and gain perspectives that were different from people who follow the main roads. Part of where coming from: working with gear as a composer (e.g., Yamaha DX-7 or the Native Instruments FM-7/FM-8, or various DAW's) and being annoyed that I couldn't experiment with ideas unless the capabilities were implemented. Didn't like the notion of my creative ideas being constrained by decisions made by engineers. Wanted, therefore, to make my own tools. Unclear yet whether it will lead to a job in the industry. I'm not exactly regretting the choice, but feel like I have a long ways to go yet to earn income from game coding. But part of that could just be the near impossibility of competing in an industry flooded with content.

 54 
 on: 2017-06-17 23:48:04 
Started by BurntPizza - Last post by Ecumene


I finally figured out a way to achieve these arcs in maps with blender, I don't know why metaballs exist but it was exactly what I was looking for!!

Now to make it look like an actual game map (which I'm not very good at..)

 55 
 on: 2017-06-17 23:46:15 
Started by Jotunn-nooks - Last post by LiquidNitrogen
https://github.com/libgdx/libgdx/wiki

This is the best starting place for working out how to do things.

 56 
 on: 2017-06-17 15:17:51 
Started by Jotunn-nooks - Last post by Jotunn-nooks
Thanks for the responses!

For libgdx, should I just look over the javadocs to try and understand, or would you recommend a tutorial course like udemy?

I think part of my conflict comes from my roommate. He actually has been doing little game projects in unity by himself and he's just about to publish one to Steam, but he doesn't really have a deep understanding of any single language, so unity works for him.

 57 
 on: 2017-06-17 01:18:17 
Started by Jotunn-nooks - Last post by SwampChicken
I can relate to this.

The only thing i can suggest is that at some point. Start working on a game that you AND ONLY YOU would enjoy. Don't try to make one that will make you millions or would have the biggest market or is on the most popular platform as you will unlikely finish it.

Think of a small/simple game that your making for your yourself, and you'll be pleasantly surprised on how much enjoyment you get get back from it as you slowly work through the hurdles and watch it come to life. My personal favorites are the tile-based game that you would get on the old Nintendo DS. Games like 'Advance Wars'. Games like these are totally do-able in java and can be built by one person.

Good luck.

 58 
 on: 2017-06-16 21:02:10 
Started by Jotunn-nooks - Last post by Gornova
start from pong with libgdx.
easy to do, you can learn a lot. Then ask yourself: "do you want to create a game or a library?" general advice here is not to waste time and go with "create game path", but you are free to do what do you want Cheesy

 59 
 on: 2017-06-16 11:07:20 
Started by wessles - Last post by J0
So last time I mentioned a webcomic; I'm actually planning on making a bunch of short stories in order to practice my art for my bigger project Smiley
I set up a website for me to upload these short stories as I go, and will also probably try to improve the web design with each iteration. So far one completed comic I drew last January, and one page of another comic I started working on a week ago or so.
If you would like to check these out: https://4gotten.000webhostapp.com/ Grin
I hope I'll be able to quickly improve!

 60 
 on: 2017-06-16 10:05:18 
Started by SteveSmith - Last post by SteveSmith
I've just released this game.  Download is at http://gamejolt.com/games/MultiplayerPlatformer/263018

Pages: 1 ... 4 5 [6] 7 8 ... 10
 
Archive (336 views)
2017-04-27 17:45:51

buddyBro (534 views)
2017-04-05 03:38:00

CopyableCougar4 (977 views)
2017-03-24 15:39:42

theagentd (1012 views)
2017-03-24 15:32:08

Rule (992 views)
2017-03-19 12:43:22

Rule (972 views)
2017-03-19 12:42:17

Rule (971 views)
2017-03-19 12:36:21

theagentd (1069 views)
2017-03-16 05:07:07

theagentd (990 views)
2017-03-15 22:37:06

theagentd (759 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!