Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Screen Update API: final draft

This thread is locked; no one can reply to it. rss feed Print
Screen Update API: final draft
23yrold3yrold
Member #1,134
March 2001
avatar

EDIT: See posts below for the latest attached update

Keen observers may have seen this already. I want to submit my screen update API too, so I gave it a quick once-over and added some new functionality, and made it as C friendly as I can. I'd like the code checked over before I formally submit it though, since it's longer (and the possibility that I missed a bug has thus gone up), but especially since I want to make sure it's okay for use in C programs (and for the life of me, I can't get my makefile to compile C source to check for myself). I'm pretty sure some sort of extern C should be in there somewhere, but I forget what and where. :-[

The source is pretty condensed; less than 300 lines of code total across 3 files (implementation .cpp, declaration .h, and the main .cpp example program). Just for kicks, I also set the color depth and default update method using a config file, if that sort of idea appeals to you.

Thanks for your time.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

Looking good!

But change the BOOL type to char to make it work with C.

:)

23yrold3yrold
Member #1,134
March 2001
avatar

You mean the waitforvsync variable? Good point. I changed the updatemethod variable to char too, just because. :) Also, I assume // comments are okay? And what about extern C; do I still have to do that somewhere? I expected more feedback. :( Thanks for checking it, RP.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

// comments were adopted as part of the c99 standed (I think), anyway I use them and they are ok.

Sunday is always a bit quiet.. :)

axilmar
Member #1,204
April 2001

Here is my own screen update API, complete with support for DRS (with a very fast clever system that avoids blitting of the same area twice without using rectangle combining/splitting) as well as all the traditional methods for screen updating, along with a little DRS demo.

EDIT:

I forgot to say that it also includes support for writing balanced game loops (the known algorithm in the Allegro FAQ), and that it is 283 lines of code, non-condensed, containing comments for Doc++ (you can find the reference at doc/index.htm).

spellcaster
Member #1,493
September 2001
avatar

axilmar: bad puppy. If you want to present your api, create your own thread. Stop that BAFism now.

23: You should check if cplusplpus is not defined:

#ifndef __cplusplus
    extern "C" {
#endif

// ...

#ifndef __cplusplus
    }
#endif

--
There are no stupid questions, but there are a lot of inquisitive idiots.

23yrold3yrold
Member #1,134
March 2001
avatar

Right; that. So I should change the header to this?

1 
2#define TRIPLEBUFFER 1
3#define PAGEFLIP 2
4#define SYSTEMBUFFER 3
5#define DOUBLEBUFFER 4
6 
7#ifndef __cplusplus
8 extern "C" {
9#endif
10
11char InitializeScreenUpdate(char i);
12void UpdateScreen();
13BITMAP* GetBuffer();
14void EnableVSync();
15void DisableVSync();
16char GetUpdateMethod();
17void ShutDownScreenUpdate();
18
19#ifndef __cplusplus
20 }
21#endif

And if the implementation file is a .cpp file (as it is now), that's not necessary, is it? If it's changed to a .c file, that's needed? I can't get my makefile to compile .c files so I can't check. :(

axilmar makes a good point, that my API has no DRS. But a DRS can be built on top of it easily enough. :) I have no need for a DRS personally.

Here's my makefile:

1# Generic Makefile.
2TEMP = $(wildcard *.cpp, *.c)
3FILES = $(if $(TEMP), $(TEMP), $(error No source code found!))
4OBJS = $(addsuffix .o, $(basename $(FILES)))
5 
6# binary name
7BINARY = ScreenUpdate.exe
8 
9# compiler flags
10FLAGS = -Wall -O2 -s -ffast-math -fomit-frame-pointer -funroll-loops
11 
12# main rule; compiles and links everything
13$(BINARY) : $(OBJS)
14 $(CXX) -o $(BINARY) $(OBJS) $(FLAGS) -mwindows -lalleg
15 
16# compiles all .cpp files, recompiles all if a header has changed,
17# and redirects errors to a log (if need be)
18%.o : %.cpp *.h
19 $(CXX) -o $@ -c $<

Here's my error:

   >make
   cc    -c -o screen.o screen.c
   process_begin: CreateProcess((null), cc -c -o screen.o screen.c, ...) failed.
   make (e=2): The system cannot find the file specified.

   make: *** [screen.o] Error 2
   >Exit code: 2

Anyone help me?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

Quote:

%.o : %.cpp *.h

Change to:

Quote:

%.o : %.c *.h

?

flares
Member #3,463
April 2003
avatar

here's my error said:

>make cc -c -o screen.o screen.c process_begin: CreateProcess((null), cc -c -o screen.o screen.c, ...) failed. make (e=2): The system cannot find the file specified. make: *** [screen.o] Error 2 >Exit code: 2

1. are all your files cpp sources or is there c source ones too?
2. im guessing you are using mingw, so set the variable CXX=g++ and CC=gcc, because it seems as if make is having trouble running cc.exe also it might help to echo out the variables like echo $(CXX) to see what file it is pointing to. you usually get the create proccess error when make can't find a file.

[nonnus29]Plus the api is crap ... I'd rather chew broken glass then code with those.

Oscar Giner
Member #2,207
April 2002
avatar

If you want to be able to compile .c and .cpp files in a same project, just have this in your makefile:

$(OBJ_DIR)/%.o: src/%.cpp
  g++ $(CFLAGS) -o $@ -c $<

$(OBJ_DIR)/%.o: src/%.c
  gcc $(CFLAGS) -o $@ -c $<

works for me :)

23yrold3yrold
Member #1,134
March 2001
avatar

Same error; sorry. :( Maybe I just don't have cc.exe ... or gcc ... or something ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

/me sees the following:

#define TRIPLEBUFFER 1
#define PAGEFLIP     2
#define SYSTEMBUFFER 3
#define DOUBLEBUFFER 4

<C0CKS SHOTGUN>
Try this before I blow your brains out:

enum {
        TRIPLEBUFFER = 1,
        PAGEFLIP     = 2,
        SYSTEMBUFFER = 3,
        DOUBLEBUFFER = 4
};

After that, I'm going after Matthew and his censorship filter.

23yrold3yrold
Member #1,134
March 2001
avatar

Yeah, I considered that. Just didn't think it was a big issue; I thought everyone here loved #define. ;D

I'd make it this though ...

enum {
        NOUPDATEMETHOD = 0,
        TRIPLEBUFFER   = 1,
        PAGEFLIP       = 2,
        SYSTEMBUFFER   = 3,
        DOUBLEBUFFER   = 4
};

... because the invalid method is handy in screen.cpp.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

You might as well drop the values then as you're not using them.

spellcaster
Member #1,493
September 2001
avatar

Quote:

Yeah, I considered that. Just didn't think it was a big issue;

It isn't a big issue. But you shouldn't use defines for constant values.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Gnatinator
Member #2,330
May 2002
avatar

All hail Barry, King of the Kode! ;D:-/;)

Nice work 23, this resource will prove useful to many.

ReyBrujo
Moderator
January 2001
avatar

23, define CXX as g++, by default it seems to be set to cc, which is a very old name of the GCC compiler (CXX = g++ at the begining of your makefile), as said above. If it stills triggers CXX, then use GCC instead of CXX, or just hardcode the compiler as Oscar suggested.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

23yrold3yrold
Member #1,134
March 2001
avatar

RB: No good. :(

Okay, here's draft 2. The screen.cpp file is now a .c file, and I'm including no makefile, so if you can't compile it without error yourself, tell me why. :)

If the code is all good, the subject of naming conventions may be introduced ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

ReyBrujo
Moderator
January 2001
avatar

Ok, got the error. The problem is this rule:

%.o : %.cpp *.h
    $(CXX) -o $@ -c $<

Note that it tells how to compile cpp files, but you have c files, so the compiler goes to the implicit default rule for c files (which is legacy, using cc for compiling). So, change that rule to:

%.o : %.c *.h
    $(CXX) -o $@ -c $< $(CFLAGS)

And should work.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

23yrold3yrold
Member #1,134
March 2001
avatar

Hooray!

1 
2# Generic Makefile.
3TEMP = $(wildcard *.cpp *.c)
4FILES = $(if $(TEMP), $(TEMP), $(error No source code found!))
5OBJS = $(addsuffix .o, $(basename $(FILES)))
6 
7# binary name
8BINARY = ScreenUpdate.exe
9 
10# compiler flags
11FLAGS = -Wall -O2 -s -ffast-math -fomit-frame-pointer -funroll-loops
12 
13# main rule; compiles and links everything
14$(BINARY) : $(OBJS)
15 $(CXX) -o $(BINARY) $(OBJS) $(FLAGS) -mwindows -lalleg
16 
17# compiles all .cpp files, recompiles all if a header has changed,
18# and redirects errors to a log (if need be)
19%.o : %.c *.h
20 $(CXX) -o $@ -c $< $(CFLAGS)

That did it. main.cpp and screen.c compile and link perfectly; thanks.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

ReyBrujo
Moderator
January 2001
avatar

I would use some different makefile, that creates a library and/or the example, like:

1LIBSRCS = screen.c
2LIBOBJS = $(addsuffix .o, $(basename $(LIBSRCS)))
3LIBNAME = libscr.a
4 
5EXASRCS = main.cpp
6EXAOBJS = $(addsuffix .o, $(basename $(EXASRCS)))
7EXANAME = ScreenUpdate.exe
8 
9CFLAGS = -Wall -O2 -s -ffast-math -fomit-frame-pointer -funroll-loops
10 
11all : library example
12 
13library : $(LIBNAME)
14 
15example : $(LIBNAME) $(EXANAME)
16 
17$(LIBNAME) : $(LIBOBJS)
18 ar ru $@ $<
19 ranlib $@
20 
21$(EXANAME) : $(EXAOBJS)
22 $(CXX) -o $@ $(EXAOBJS) $(CFLAGS) -mwindows $(LIBNAME) -lalleg
23 
24%.o : %.c *.h
25 $(CXX) -o $@ -c $< $(CFLAGS)
26 
27%.o : %.cpp *.h
28 $(CXX) -o $@ -c $< $(CFLAGS)

Also, my mistake in your previous makefile: Use all FLAGS or all CFLAGS, otherwise it would compile and link with different flags. Convention says you should use CFLAGS for C and CXXFLAGS for C++, but in this case it is up to you.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

axilmar
Member #1,204
April 2001

Quote:

axilmar: bad puppy. If you want to present your api, create your own thread. Stop that BAFism now.

Ok, if that's the proper way.

Steve Terry
Member #1,989
March 2002
avatar

o_O there is competiton today of screen updaters ;D I would post mine... but nah. Anyway my question is what is the advantage/reason for using enum instead of #define?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Kanzure
Member #3,669
July 2003
avatar

..edit..

Oh, and it just looks cooler to boot ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

#define is pre processor based. they get replaced inline with the values before the compiler even gets the code.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730



Go to: