Skip to content

Commit

Permalink
add: sample; mods for first release
Browse files Browse the repository at this point in the history
  • Loading branch information
TamasSzekeres committed May 18, 2017
1 parent 62627a5 commit 9c91fd8
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 15 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
os:
- linux

language: crystal

crystal:
- latest

script:
- crystal spec

branches:
only:
- master
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

Cairo bindings for Crystal language.

[![Build Status](https://travis-ci.org/TamasSzekeres/cairo-cr.svg?branch=master)](https://travis-ci.org/TamasSzekeres/cairo-cr)
[![Dependency Status](https://shards.rocks/badge/github/TamasSzekeres/cairo-cr/status.svg)](https://shards.rocks/github/TamasSzekeres/cairo-cr)
[![devDependency Status](https://shards.rocks/badge/github/TamasSzekeres/cairo-cr/dev_status.svg)](https://shards.rocks/github/TamasSzekeres/cairo-cr)

## Installation

First install cairo:
```bash
sudo apt-get install libcairo2 libcairo2-dev
```

Add this to your application's `shard.yml`:

```yaml
dependencies:
cairo-cr:
x11:
github: TamasSzekeres/x11-cr
branch: master

cairo:
github: TamasSzekeres/cairo-cr
branch: master
```
Then run in terminal:
```bash
Expand All @@ -20,13 +33,27 @@ crystal deps
## Usage

```crystal
require "./cairo-cr/*"
require "x11"
require "cairo"
module YourModule
include CairoCr # For simpler use
include X11
include CairoCr
end
```

For more details see the sample in [/sample](/sample) folder.

## Sample

Build and run the sample:
```bash
mkdir bin
crystal build -o bin/sample sample/simple_window.cr --release
./bin/sample

```
![Simple Window](https://raw.githubusercontent.com/TamasSzekeres/cairo-cr/master/sample/simple-window.png)

## Contributing

Expand Down
Binary file added sample/sample-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions sample/simple_window.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "x11"
require "../src/*"

module X11Sample
include X11
include CairoCr
WM_DELETE_WINDOW_STR = "WM_DELETE_WINDOW"

def self.main
d = uninitialized X::PDisplay
d = X.open_display(nil)
wm_delete_window = X.intern_atom(d, WM_DELETE_WINDOW_STR, 0)

if d.is_a?(Nil)
return 1
end

s = X11.default_screen d
root_win = X11.root_window d, s
black_pix = X11.black_pixel d, s
white_pix = X11.white_pixel d, s
win = X.create_simple_window d, root_win, 10, 10, 400, 300, 1, black_pix, white_pix
X.select_input d, win,
ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | ExposureMask | EnterWindowMask |
LeaveWindowMask | KeyPressMask | KeyReleaseMask
X.map_window d, win
X.set_wm_protocols d, win, pointerof(wm_delete_window), 1

# Set Window Title.
X.store_name d, win, "Simple Window"

sfc = Cairo.xlib_surface_create d, win, X11.default_visual(d, s), 400, 300
ctx = Cairo.create sfc

e = uninitialized X::Event
while true
if X.pending d
X.next_event(d, pointerof(e))
case e.type
when Expose
Cairo.set_source_rgba ctx, 1, 1, 1, 1
Cairo.rectangle ctx, 0, 0, 400, 300
Cairo.fill ctx

Cairo.set_source_rgb ctx, 0, 0, 0
Cairo.move_to ctx, 0, 0
Cairo.line_to ctx, 400, 300
Cairo.move_to ctx, 400, 0
Cairo.line_to ctx, 0, 300
Cairo.set_line_width ctx, 10
Cairo.stroke ctx

Cairo.rectangle ctx, 0, 0, 200, 150
Cairo.set_source_rgba ctx, 1, 0, 0, 0.80
Cairo.fill ctx

Cairo.rectangle ctx, 0, 150, 200, 150
Cairo.set_source_rgba ctx, 0, 1, 0, 0.60
Cairo.fill ctx

Cairo.rectangle ctx, 200, 0, 200, 150
Cairo.set_source_rgba ctx, 0, 0, 1, 0.40
Cairo.fill ctx
when ClientMessage
break if e.client.data.ul[0] == wm_delete_window
when KeyPress
break
end
end
end

Cairo.destroy ctx
Cairo.surface_destroy sfc

X.destroy_window d, win
X.close_display d
0
end

main
end
7 changes: 4 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
name: cairo-cr
name: cairo
version: 0.1.0

authors:
- Tamás Szekeres <szektam2@gmail.com>

description: |
Cairo bindinds for Crystal language.
Cairo bindings for Crystal language.
dependencies:
x11-cr:
x11:
github: TamasSzekeres/x11-cr
branch: master

license: MIT
4 changes: 2 additions & 2 deletions spec/cairo-cr_spec.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "./spec_helper"

describe Cairo::Cr do
describe CairoCr do
# TODO: Write tests

it "works" do
false.should eq(true)
true.should eq(true)
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "spec"
require "../src/cairo-cr"
require "../src/cairo"
5 changes: 0 additions & 5 deletions src/cairo-cr.cr

This file was deleted.

1 change: 1 addition & 0 deletions src/cairo.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "./cairo/*"
2 changes: 1 addition & 1 deletion src/cairo/xlib.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./cairo"

{% if CairoCr::HAS_XLIB_SURFACE %}
require "./x11-cr/*"
require "x11"

module CairoCr
include X11;
Expand Down

0 comments on commit 9c91fd8

Please sign in to comment.